How-To Guide

How to Write Portable Cross-Platform Scripts

Write scripts that run on Linux, macOS, and Windows: use a real script runner, avoid the shell differences, and test each platform.

On this page

  1. Introduction
  2. Prefer a cross-platform runner
  3. Avoid the shell-specific constructs
  4. Handle the paths and the line endings
  5. Test on each platform
  6. Test the script on each platform
  7. Common pitfalls
  8. Conclusion

Introduction

A script that runs on Linux but fails on Windows adds a platform to the maintenance and blocks a contributor who uses a different system. The differences are concrete: the shell, the path separators, the line endings, the available utilities, and the environment. A portable script uses a runner that is the same everywhere and avoids the shell-specific constructs, and it is tested on each platform.

This tutorial covers using a portable runner, avoiding the shell-specific constructs, handling the paths and the line endings, and testing each platform. The examples use a cross-platform runner, and the design applies to any script a team shares.

Prefer a cross-platform runner

Prefer a runner such as Node.js or Python, which is the same on Linux, macOS, and Windows, over a shell script, which is the shell’s. The runner gives a path API, a file API, and a process API that abstract the platform’s differences, so the script does not special-case each OS. A script in the runner is a script on every platform.

A shell script is still useful for the local tasks the developer runs, but the shared, automated tasks, especially the CI, are better in a runner. The runner also gives the error handling and the libraries the shell lacks. Choose the runner for the shared scripts, and reserve the shell for the interactive convenience.

import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { readFileSync } from 'node:fs';

const here = dirname(fileURLToPath(import.meta.url));   // works on every platform
const config = join(here, 'config', 'app.json');          // the path separator is handled
const data = JSON.parse(readFileSync(config, 'utf8'));

Avoid the shell-specific constructs

When a shell is required, stick to the POSIX constructs and avoid the bash-only features that another shell does not support: the arrays, the [[ ]] test, the local keyword, the process substitution. A script that uses them runs in bash and fails in a POSIX sh, which is a different shell. The POSIX subset is the portable one.

The shebang also matters: #!/bin/sh is the POSIX shell, and #!/usr/bin/env bash is bash, which may not be installed. Choose the shebang for the interpreter the script needs, and do not assume bash is present. The portable choice is the POSIX sh, or a runner, not a bash feature the script depends on.

#!/bin/sh
# POSIX constructs: no [[ ]], no arrays, no local
set -e
for f in "$@"; do
  case "$f" in
    *.txt) echo "text: $f" ;;
  esac
done

Handle the paths and the line endings

Use the path API of the runner, or the POSIX path, rather than the concatenation with a hardcoded separator. A script that joins with a forward slash breaks on Windows, and one that joins with a backslash breaks on Unix. The path API produces the platform’s separator, so the script runs on each.

The line endings are a classic cross-platform failure: a script written on Windows has the carriage returns, and the Unix shell reads \r as part of the command. A .gitattributes that normalizes the text files to LF, and a config that writes LF, keep the line endings consistent, so a script checked out on Linux does not carry the \r.

# .gitattributes: normalize the text files to LF, regardless of the OS
* text=auto eol=lf
*.sh text eol=lf
*.bat text eol=crlf

Test on each platform

Run the script in the CI on Linux, macOS, and Windows, so a platform-specific failure is caught before the release. A matrix build runs the same script on each, and the failures are visible per platform. A script tested on one platform and released breaks on the others, which the matrix prevents.

Cover the platform-specific paths: the path separator, the home directory, the temp directory, the executable suffix, and the line endings. A test that runs the script on each platform and asserts its effect catches a platform assumption. The CI is where the portability is verified, because the developer’s machine is one platform.

# CI matrix: run the script on each platform
strategy:
  matrix:
    os: [ubuntu-latest, macos-latest, windows-latest]
steps:
  - uses: actions/checkout@v4
  - run: node scripts/build.mjs

Test the script on each platform

Test that the script runs on each platform, by running it in the CI matrix and asserting the effect. Test the path handling, by a case that joins a nested path and asserts the result on the platform. Test the line endings, by asserting the checked-out script has the LF, so the \r failure is caught. These tests keep the script portable as it changes.

# assert the checked-out shell script has no carriage return
if grep -q $'\r' scripts/build.sh; then echo "CRLF found"; exit 1; fi
# the CI matrix runs the script on Linux, macOS, and Windows

Common pitfalls

A bash-only script that fails in another shell, the path concatenation with a hardcoded separator, the line endings from a Windows checkout read as \r on Linux, the assumption that a Unix utility is present on Windows, and testing on one platform only are the recurring mistakes. A cross-platform runner, the POSIX constructs, the path API, the line-ending normalization, and the CI matrix address them.

A further portability trap is the environment and the tool availability: a script that assumes node, python, or a specific version is on the path, or that a utility such as sed, awk, or find behaves the same, breaks on a machine where the tool is absent or different. Declare the required runtime version in the project config, use the runner’s own APIs instead of shelling out to a utility, and let the CI matrix install the pinned version, so the script depends on a declared environment rather than on whatever the developer’s machine happens to have.

Conclusion

A portable script runs on every platform the team uses. Prefer a cross-platform runner, avoid the shell-specific constructs, use the path API and normalize the line endings, and test on each platform in the CI matrix. Self-check by running the script on Linux, macOS, and Windows and asserting the same effect. Then the script does not block a contributor on a different system.

Sources: Node.js: Path and OS modules, Git: gitattributes and line endings, Python: os.path and pathlib portability.

FAQ

Why not a bash script?
A bash script depends on bash and the Unix utilities, which differ or are absent on Windows, and a different shell may not support the same features. A cross-platform runner such as Node.js or Python runs the same on every platform, so the script does not fight the shell.
How do I keep line endings consistent?
Use a .gitattributes that normalizes the line endings, and configure the editors and the tools, so a script written on Windows and run on Linux does not break on a carriage return. The line ending is a classic cross-platform failure.

Related Examples