Portability: POSIX sh and Bash
Decide whether a script targets POSIX sh or Bash, find bashisms with ShellCheck, and see what /bin/sh, dash and the macOS Bash 3.2 each accept. Tested on macOS.
Your first script - shebang and execute bitConditionals with if, test and double bracketsArrays and associative arraysParameter expansionDebugging with set -x and ShellCheck
Portability: POSIX sh and Bash
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- POSIX sh is the common core every Unix shell must provide. Bash is a superset with extras such as [[ ]], arrays, ${var,,}, <(...) and mapfile, which are bashisms.
- The shebang decides which program reads your script. #!/bin/sh promises POSIX only, and #!/usr/bin/env bash declares that you need Bash, and which Bash is first on PATH.
- /bin/sh is not the same shell everywhere: dash on Debian and Ubuntu, and Bash 3.2 in POSIX mode on macOS. Testing under only one of them hides bugs.
- macOS ships Bash 3.2 as /bin/bash, from 2007. It has no associative arrays, no ${var^^} and no mapfile, so a Bash 4+ script can fail there.
- shellcheck --shell=sh script finds bashisms (the SC3xxx warnings). For most scripts the practical choice is to write Bash on purpose and say so in the shebang.
What the shebang promises
A script is a request to some interpreter. The first line says which one, and what you are promising to write.
#!/bin/sh promises POSIX sh, the small shell language the POSIX standard defines, which every Unix has in some form. #!/usr/bin/env bash says “I use Bash features”. The trouble is that /bin/sh is a different program on different systems, and some of them are lenient. A script can “work” for years on one machine and break on the next because it was never actually POSIX.
I tested on macOS with four shells: Bash 5.3 from Homebrew, /bin/bash (3.2.57), /bin/sh (also Bash 3.2, running in POSIX mode) and /bin/dash, which macOS also ships and which is the same family as Debian and Ubuntu’s /bin/sh.
One script, four shells
This script has #!/bin/sh on top and is full of Bash features:
#!/bin/sh
name="World"
if [[ $name == W* ]]; then echo "match"; fi
files=(a b c)
echo "${files[1]}"
echo "${name,,}"
echo -e "a\tb"
diff <(echo 1) <(echo 1) && echo same
Results:
=== under Bash 5.3
match
b
world
a b
same
=== under /bin/bash (3.2)
match
b
bashy.sh: line 6: ${name,,}: bad substitution
a b
same
=== under /bin/sh (Bash 3.2, POSIX mode)
match
b
bashy.sh: line 6: ${name,,}: bad substitution
-e a b
bashy.sh: line 8: syntax error near unexpected token `('
=== under /bin/dash
bashy.sh: 3: [[: not found
bashy.sh: 4: Syntax error: "(" unexpected
Read it line by line. Bash 5.3 runs all of it. Bash 3.2 is missing ${name,,} (lowercasing arrived in Bash 4). As /bin/sh, Bash 3.2 still accepts [[ ]] and arrays but drops process substitution, and echo -e prints a literal -e. dash rejects the very first [[, and stops at the array as a syntax error before running anything past it. On a Mac, the #!/bin/sh lie survives most of the script, which is exactly how it ships.
A POSIX version of the same script:
#!/bin/sh
name="World"
case $name in W*) echo "match" ;; esac
set -- a b c
echo "$2"
echo "$name" | tr '[:upper:]' '[:lower:]'
printf 'a\tb\n'
It produced identical output under Bash 5.3, /bin/sh and dash. The substitutions: case for pattern tests, positional parameters (set --) as the one array POSIX has, tr for case, and printf instead of echo -e, whose behaviour varies. In my runs echo -e printed a stray -e under both /bin/sh and dash and worked under Bash.
Let ShellCheck find them
You do not need four shells to find bashisms. The ShellCheck lesson tool reads the shebang and flags what that shell does not promise:
$ shellcheck bashy.sh
In bashy.sh line 3:
^---------------^ SC3010 (warning): In POSIX sh, [[ ]] is undefined.
^-- SC3014 (warning): In POSIX sh, == in place of = is undefined.
In bashy.sh line 4:
^-----^ SC3030 (warning): In POSIX sh, arrays are undefined.
...
SC3059 (warning): In POSIX sh, case modification is undefined.
SC3037 (warning): In POSIX sh, echo flags are undefined.
(Source excerpts trimmed.) Running shellcheck posix.sh printed nothing, meaning clean. For a script with a Bash shebang, shellcheck --shell=sh script checks it against POSIX anyway, and flagged the array and [[ in my test.
The Bash version gap
The /bin/bash problem is separate. macOS still ships Bash 3.2.57, a 2007 release frozen at the last GPLv2 version. Features added in Bash 4 and later fail there:
$ /bin/bash -c 'declare -A m; m[a]=1; echo ${m[a]}'
/bin/bash: line 0: declare: -A: invalid option
$ /bin/bash -c 'x=abc; echo ${x^^}'
/bin/bash: ${x^^}: bad substitution
$ /bin/bash -c 'mapfile -t a < /dev/null'
/bin/bash: mapfile: command not found
The same three lines worked in Bash 5.3 (it printed 1 and ABC). So a script using the associative arrays from earlier in this roadmap will not run under macOS’s own /bin/bash. #!/usr/bin/env bash picks whichever Bash comes first on PATH, so a Homebrew-installed one wins if the PATH puts it first. A cron job or a launcher with a short PATH might still reach 3.2. Check with echo "$BASH_VERSION" at the top when in doubt.
Choosing
The honest rule: pick a target, state it in the shebang, and test against it.
- Bash on purpose.
#!/usr/bin/env bash, use the features, and document the minimum version. Right for almost all scripts on machines you control. - POSIX sh.
#!/bin/sh,shellcheck, and run it underdash(dash script.sh). Right for installers, container entrypoints and anything that runs where Bash may not exist, such as minimal Alpine images, which I did not test. - Neither is a mistake. The mistake is a
#!/bin/shheader over Bash code.
The local keyword is not in the POSIX standard but is accepted by dash, which I confirmed (local x=1 printed 1 under dash), and by almost every /bin/sh. So you will see it in “POSIX” scripts. bash --posix turns on POSIX behaviour in Bash itself, and shopt -o posix confirmed it was on, but it is not a promise that the script is portable, because Bash still accepts many bashisms in that mode.
About the video
“I’m BREAKING UP With Bash (Using dash as /bin/sh)” is a 12-minute 41-second video from Eric Murphy, published October 2022. The description says it shows how to change the system /bin/sh to dash and how to write portable, POSIX-compliant scripts that are not Bash-only, and it links an article on checking for bashisms and testing with dash. I could not read a transcript. It is an English-language channel. The framing is opinionated, in favour of dash for speed and portability, and that preference is not required by this lesson. Its system-shell change is specific to Debian-style Linux, and I did not try it. Nick Janetakis’s short ShellCheck video on the --shell sh flag is linked as a second view.
Where this fits
Once you know what you are targeting, the next question is how to lay a script out so it stays readable: structure and style.
Check yourself
Your script starts with #!/bin/sh and uses [[ ]]. It works on your Mac and fails on Ubuntu. Why?
On macOS /bin/sh is Bash in POSIX mode, which still accepts [[ ]]. On Ubuntu it is dash, which does not know [[ ]]. Use [ ] or case, or change the shebang to Bash.
How do you check a script for non-POSIX syntax without running it elsewhere?
Run shellcheck --shell=sh script.sh. It reports bashisms such as arrays and [[ ]] as SC3xxx warnings.
A teammate on a Mac runs your script and gets 'declare: -A: invalid option'. What happened?
/bin/bash on macOS is version 3.2, which has no associative arrays. Run it with a newer Bash, or avoid declare -A.
When is it fine to just use Bash features?
When the script declares Bash in its shebang and the target machines are known to have a recent enough Bash. Portability to bare sh only matters for scripts that must run in minimal environments such as containers or system installers.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.