$_ CLI In Depth
Learning progress 0%
lesson robustness traps debugging

Portable sh vs bash

4 min read Module 15: Robust Scripts, Traps & Debugging Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:27

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.

BASH1 lines
if [[ "$x" == yes ]]; then echo ok; fi

This 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.

BASH1 lines
if [ "$x" = yes ]; then echo ok; fi

Choosing 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.

Warning

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.

BASH2 lines
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.

BASH3 lines
#!/bin/sh
n=$#
if [ "$n" -eq 0 ]; then echo none; else echo "count=$n"; fi

This 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.

BASH1 lines
sh -c 'case "$1" in yes|y) echo affirmative ;; *) echo negative ;; esac' sh yes

The 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.

Terminal lab · 2 steps · real Linux in your browser

Write a POSIX sh script

Write two scripts that use only POSIX features and run them with sh.

01
Use case in a POSIX script
Create posix.sh in /workspace. Start it with #!/bin/sh. Use a case statement that prints affirmative for yes or y, and negative for anything else: ``bash cat > posix.sh <<'EOF' #!/bin/sh case "$1" in yes|y) echo affirmative ;; *) echo negative ;; esac EOF `` Run it with sh posix.sh yes and sh posix.sh no.
Show hint for step 1
The case patterns are separated with a vertical bar. Quote the argument in the case word.
02
Use single brackets and $#
Create count.sh in /workspace. Start it with #!/bin/sh. Count its arguments with $#, and print none when there are none, otherwise count=N. Use the single bracket test with -eq: ``bash cat > count.sh <<'EOF' #!/bin/sh n=$# if [ "$n" -eq 0 ]; then echo none; else echo "count=$n"; fi EOF `` Run it with no arguments and with two arguments.
Show hint for step 2
Use [ ] instead of [[ ]], and -eq for numbers. $# is the argument count.
Check yourself Question 1 of 4
Concept Check A POSIX string comparison recall

Which test is the POSIX form for comparing a string to yes?

Recap & Key Takeaways

POSIX sh defines a fixed shell language, and bash adds many extensions that are not portable to other shells.
Common bashisms include double brackets, arrays, function with the keyword, process substitution, and the == operator in single brackets.
Choose #!/bin/sh only when the script is tested under that shell, and #!/usr/bin/env bash when it uses bash features.
printf, $(...), [ ] with =, and quoted expansions are portable habits that keep scripts working across shells.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue