POSIX Basics
Shared rules help scripts run on different computers
Imagine writing a short shell script on one computer and wanting it to work on another. POSIX is a shared rulebook for how programs ask an operating system to do common jobs, how shell scripts are written, and how standard commands behave. When two systems follow the same POSIX rules, a script can often run on both with fewer changes. Linux distributions may add their own package tools, service managers, filesystem conventions, or GNU features that POSIX does not cover.
Here is a small script that looks for log files below a directory supplied as the first argument:
#!/bin/sh
set -eu
root=$1
find "$root" -type f -name '*.log' -print
The line #!/bin/sh asks the system to run the file with its sh shell. The quoted "$root" keeps a directory name with spaces as one argument. The quotes around '*.log' prevent the shell from expanding the star against files in the current directory; find receives the pattern and matches names as it walks the tree.
Decide which shell features you need
Bash arrays, [[ ... ]], process substitution, and pipefail are Bash features. A script that uses them should name Bash in its interpreter line. For a script that needs only portable shell features, #!/bin/sh is appropriate. On many Linux systems, /bin/sh points to a shell other than Bash.
The script also uses set -e and set -u. The first makes the shell exit after many command failures; the second reports use of an unset variable. These options do not replace explicit handling for failures the script expects and may need to recover from. Try scripts with their declared shell and with paths containing spaces.
Utilities have portability limits too. POSIX may require a utility while an option available on one machine is a feature added by GNU tools. Add predicates such as -mtime, -size, or -user to narrow a find search, but check the utility specification and target manual when the script must run on several systems. GNU Findutils has features beyond the POSIX baseline.
Check the system you will run on
POSIX specifies behavior for interfaces and utilities. It does not prescribe a kernel's internal design, and some behavior is left unspecified. Implementations may also document limits or add extensions. When portability matters, check the shell and utility versions on the target and test there. POSIX standards have editions, and older systems may implement an earlier one.
A script may intentionally require Bash or GNU tools. State those dependencies in its interpreter line and deployment requirements. When the script must work across POSIX-conforming systems, stay within the specified shell language and utility behavior.
Interactive exercises
Materials
- POSIX - WikipediaBasic description of POSIX







