# Portability talk  The script programming language of BASH is based on the Bourne Shell syntax, with some extensions and derivations. If scripts need to be portable, some of the BASH-specific syntax elements should be avoided. Others should be avoided for all scripts, e.g. if there is a corresponding POSIX(r)-compatible syntax (see [obsolete](/scripting/obsolete)). Some syntax elements have a BASH-specific, and a portable[^1]) pendant. In these cases the portable syntax should be preferred.
construct | portable equivalent | Description | Portability |
---|---|---|---|
source\ FILE |
. FILE |
include a script file | Bourne shell (bash, ksh, POSIX(r), zsh, ...) |
declare \ keyword |
typeset keyword |
define local variables (or variables with special attributes) | ksh, zsh, ..., not POSIX! |
command\ <<<\ WORD |
command <<MARKER WORD MARKER |
a here-string, a special form of the here-document, avoid it in portable scripts! | POSIX(r) |
export VAR=VALUE |
VAR=VALUE export VAR |
Though POSIX(r) allows it, some shells don't want the assignment and the exporting in one command | POSIX(r), zsh, ksh, ... |
(( MATH )) |
: $(( MATH )) |
POSIX(r) does't define an arithmetic
compund command, many shells don't know it. Using the pseudo-command
: and the arithmetic expansion $(( )) is a
kind of workaround here. Attention: Not all shell
support assignment like $(( a = 1 + 1 )) ! Also see below
for a probably more portable solution. |
all POSIX(r) compatible shells |
[[\ EXPRESSION\ ]] |
[ EXPRESSION ] or test EXPRESSION |
The Bashish test keyword is reserved by
POSIX(r), but not defined. Use the old fashioned way with the
test command. See the
classic test command |
POSIX(r) and others |
COMMAND\ <\ <(\ ...INPUTCOMMANDS...\ ) |
INPUTCOMMANDS\ >\ TEMPFILE COMMAND\ <\ TEMPFILE |
Process substitution (here used with redirection); use the old fashioned way (tempfiles) | POSIX(r) and others |
((echo X);(echo Y)) |
( (echo X); (echo Y) ) |
Nested subshells (separate the inner
() from the outer () by spaces, to not confuse
the shell regarding arithmetic control operators) |
POSIX(r) and others |