Linux Command Reference ยท Concepts
POSIX sh vs Bash
Which shell features are POSIX and which are Bash extensions (bashisms): arrays, [[ ]], brace expansion, process substitution, and portable replacements for each.
POSIX sh is the shell language defined by the POSIX standard: variables, functions, if, case, loops, pipes, redirections, $(...), $((...)) and a fixed list of builtins. Bash implements all of it and adds a long list of extensions, called bashisms. A script that sticks to the standard core runs under dash, BusyBox ash, ksh and Bash alike.
The practical question is which interpreter runs your file. On Debian and Ubuntu /bin/sh is dash, which implements little beyond POSIX, so a script starting with #!/bin/sh that uses an array fails there while working on systems where /bin/sh is Bash.
#!/bin/sh # promise: POSIX only #!/bin/bash # free to use Bash features
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Common bashisms and portable alternatives
| Bash feature | POSIX alternative |
|---|---|
[[ $a == pat* ]] |
case $a in pat*) ... ;; esac |
[[ $a =~ re ]] |
printf %s "$a" | grep -Eq re |
[[ -z $x && -n $y ]] |
[ -z "$x" ] && [ -n "$y" ] |
arr=(a b c), ${arr[@]} |
positional parameters (set -- a b c; "$@") or lines in a file |
declare -A maps |
awk arrays, or files |
{1..5} |
seq 1 5 or a while loop with $((i+1)) |
$'a\tb' |
printf 'a\tb' into a variable with $(...) |
<(cmd) |
a temporary file from mktemp, or a pipe |
cmd <<< "$s" |
printf '%s\n' "$s" | cmd or a here-doc |
${s^^}, ${s,,} |
printf %s "$s" | tr a-z A-Z |
${s:2:3}, ${s//a/b} |
cut, sed, or ${s#prefix} and ${s%suffix} (these are POSIX) |
function f { } |
f() { } |
local |
not in POSIX, but dash, ash, ksh and Bash all provide it |
source file |
. file |
echo -e, echo -n |
printf |
== in [ ] |
= |
&>file |
>file 2>&1 |
set -o pipefail |
not available in older POSIX (added in POSIX 2024); check $? of each stage manually |
$RANDOM, $SECONDS, $BASH_SOURCE |
awk 'BEGIN{srand(); print int(rand()*100)}', date +%s |
read -p, read -a |
printf prompt >&2; read -r var |
What is POSIX and handy: ${var:-default}, ${var:=x}, ${var#pat}, ${var##pat}, ${var%pat}, ${var%%pat}, ${#var}, $((...)), $(...), case, getopts, trap, set -eu, command -v, printf, read -r.
Usage notes
- Decide per script. If it runs on your own servers with Bash installed, use
#!/usr/bin/env bashand enjoy the extensions. Aim for POSIX only for installers, container entrypoints on Alpine, and scripts shipped to unknown systems. - POSIX mode: Bash invoked as
shor with--posixturns off some extensions but still accepts many bashisms, so testing under Bash-as-sh proves little. Test withdashitself (dash script.sh). - Linters: ShellCheck reads the shebang and warns about bashisms in
shscripts ("In POSIX sh, ... is undefined").checkbashisms(Debian) does similar. - macOS ships Bash 3.2, which lacks associative arrays,
mapfile,${s^^}and|&. For maximum reach target 3.2 or POSIX. - Tools matter too: GNU
sed -i,date -d,grep -Pandreadlink -fdiffer or are missing on BSD/macOS. Portability is not only the shell language. echois the worst offender: whether-nand backslash escapes work depends on the shell.printfis specified precisely and portable.localandpipefailhave been de facto portable for years even though classic POSIX did not require them; POSIX 2024 addedpipefail.