What are common "bashisms" that break under `/bin/sh`?
IntermediateConceptShellCheck, debugging, testing, portability and security
Answer
| Bash | POSIX sh alternative |
|---|---|
[[ $a == b* ]] |
case $a in b*) ... ;; esac |
arr=(a b) |
positional params set -- a b or newline-separated strings |
function f { } |
f() { } |
$'\n' |
"$(printf '\n_')" then strip the _ |
<(cmd) |
temp file or named pipe |
${var//a/b}, ${var^^} |
sed, tr |
source f |
. f |
&>file |
>file 2>&1 |
echo -e |
printf |
(( i++ )) |
i=$((i + 1)) |
Check with shellcheck -s sh or checkbashisms. Typical error under dash: [[: not found or Syntax error: "(" unexpected.
More on ShellCheck, debugging, testing, portability and security
What is ShellCheck and how do you use it in a team?BeginnerHow do you disable a ShellCheck warning, and when is that acceptable?IntermediateName the ShellCheck warnings you see most and what each means.IntermediateHow do you debug a Bash script?BeginnerHow do you make `set -x` output more useful with `PS4`?IntermediateWhat is bats, and how do you test a script that calls external commands?Intermediate