What POSIX sh defines
POSIX defines a shell language with a fixed set of features: simple commands, pipelines, lists, the if, case, for, and while constructs, parameter expansion with the basic forms, command substitution in the $(...) form, and the test command [. Every POSIX shell implements those features the same way. Anything beyond that list is an extension, and bash has many.
The POSIX shell language is specified in the Shell Command Language chapter of the standard, which you can read online. It is the reference to check when you are unsure whether a construct is portable.
Common bashisms
A bashism is a feature that works in bash but not in a POSIX shell. The most common are the double bracket test [[ ]], arrays, function definitions with the keyword, process substitution <(...), the local builtin in some shells, and the == operator inside single brackets. Each one may run fine on your machine and fail on a minimal system.
if [[ "$x" == yes ]]; then echo ok; fiThis line is valid bash. Under a POSIX shell that does not recognize [[, it fails with a syntax error or a "not found" error, depending on the shell. The portable form uses single brackets and the single equals sign, which is the comparison operator that POSIX defines.
if [ "$x" = yes ]; then echo ok; fiChoosing the shebang
The first line of a script, the shebang, selects the interpreter. #!/bin/sh asks for the system shell and promises POSIX compatibility. #!/usr/bin/env bash asks for bash wherever it lives on the search path. Choose sh only when you have tested the script under the shells you target, and choose bash when you use any bash feature. Do not use sh as a label for a script that quietly uses arrays.
A script with a sh shebang that contains a bashism may pass on your laptop when run as bash script.sh, and then fail on a server where the script is run directly. Test with the same shell that the shebang names.
Running a script under sh
You can test portability by running the script with the POSIX shell on the machine you have. On the lab machine, sh is a BusyBox shell, which is a common case for minimal Linux systems. Running a bash-only script with sh script.sh shows the failure directly.
printf "%s\n" 'if [[ 1 == 1 ]]; then echo yes; fi' > bashonly.sh
sh bashonly.sh; echo "status=$?"Whether the output is an error or a surprise depends on the shell. The status is not zero for a plain failing shell. The lesson is to test the script under the shell it names, not under the shell you prefer.
Portable habits
A few habits make a script portable without much effort. Use printf instead of echo for output with backslashes or leading dashes. Use $(...) instead of backticks. Use [ ] with = for string comparison, and -eq for numbers. Avoid arrays, unless you rewrite them as positional parameters or files. Quote every expansion, because that is portable to every shell.
#!/bin/sh
n=$#
if [ "$n" -eq 0 ]; then echo none; else echo "count=$n"; fiThis script uses only POSIX features, and it runs the same way in dash, BusyBox ash, and bash.
When bash is the right choice
Portability is not always worth the cost. If a script runs only on machines you control, and you need arrays, associative arrays, or [[ ]], declare bash in the shebang and use the features. The honest choice is the one that matches the environment. The next units assume bash, and the rest of this course uses bash syntax on purpose.
Hands-on: test a POSIX case statement
Run the block below. It uses a case statement, which is part of POSIX, and runs it with the sh on the lab machine.
sh -c 'case "$1" in yes|y) echo affirmative ;; *) echo negative ;; esac' sh yesThe output is affirmative. The lab asks you to write a POSIX script that uses case and a test with single brackets, and to run it with sh.