How to make bash scripts work in dash

This page is an attempt to list some of the most common bashisms ie features not defined by POSIX (ie don't work in dash). It probably won't be exhaustive. Note also we talk about "bashism" because this wiki is largely bash centric but a number of these extensions work in other shells like ksh or zsh.

Syntax

Works in bash

Change to for dash

Comment

defining functions

function f(){echo hello world}

f() { echo hello world ; }

"function" is not defined by posix, only "name ()" is

numeric C-like for loop

for ((i=0; i<3; i++)); do echo $i ; done

i=0 ; while test $i -lt 3 ; do echo $i ; i=$(($i+1)) ; done

the C-like for syntax (for (( ; ; ));do ..) is not defined by posix

case

;;&

;;& in bash4 is not defined by posix

case

;&

;& in bash4 is not defined by posix

select

select

select is not defined by posix

expand sequences

echo $'hello\tworld'

printf "hello\tworld"

$' ' is not defined by posix

extended glob

( +() @( ) etc...

not defined by posix

Expansions

Parameter Expansions

List of expansions not defined by posix:

Arrays

Conditionals

Arithmetic

Redirections

Builtins

more

Note that bash in posix mode is only guaranteed to run a shell written according to the posix specification. It doesn't mean that it will fail if you use bashisms in your scripts.