Concept

Portability

Definition

Portability is the property that lets a program, script, or system run unchanged — or with minimal change — across different platforms, operating systems, hardware architectures, or shell environments. A portable program does not assume the path separator is a forward slash, that the available memory is unlimited, that a specific GNU extension to a utility is present, or that the byte order is little-endian. It writes against a contract — usually a standard like POSIX, ISO C, or a language specification — rather than against the quirks of one implementation.

Portability is a spectrum, not a switch. A bash script with no extensions runs on every POSIX shell; a script that uses [[ ]] runs only on bash, zsh, and ksh; a script that calls gdate runs only where GNU coreutils are installed. Each capability used trades a small ease-of-writing benefit for a portability cost paid forever.

Why it matters

How it works

Portability is achieved through layered abstractions. At the lowest layer, the operating system kernel exposes system calls; portability across kernels is achieved by standardising the system-call interface (POSIX). Above that, the C standard library wraps system calls in functions whose behavior is defined by ISO C — code written against C can compile on any platform with a conforming compiler. Higher up, a portable shell script writes against POSIX shell utilities (sh, awk, sed, grep) rather than implementation extensions; a portable Python program writes against the standard library rather than OS-specific modules.

The discipline is largely negative: it is the set of features you decline to use. Avoid GNU-specific flags (grep -P, sed -i '' vs sed -i), avoid bash-isms in #!/bin/sh scripts, avoid hard-coded paths like /usr/bin/python, prefer printf over echo -e, and check the exit status of utilities whose semantics differ between BSD and GNU. When a non-portable feature is genuinely needed, isolate it behind a wrapper so the rest of the program stays portable.

Where it goes next

Continue exploring

Tags