Bash – What kinds of string interpolation does POSIX sh support

bashposixshellshell-script

Is "${blah}" allowed in POSIX sh, or does this require bash derived shells?

Best Answer

"${blah}" and "$blah" are portable shell syntax: they work on all POSIX-compliant shells as well as in traditional Bourne shells. POSIX also requires other features of variable expansion:

  • String manipulation with ${VAR#PREFIX}, ${VAR##PREFIX}, ${VAR%SUFFIX} and ${VAR%%SUFFIX}.
  • Conditional treatment of unset variables with ${VAR-DEFAULT}, ${VAR=DEFAULT}, ${VAR+FALLBACK} and ${VAR?MESSAGE} as well as the unset-or-empty variants with :-, :=, :+ and :?.
  • Variable length with ${#VAR}.

In all cases, remember that the result of $… undergoes whitespace-splitting (more precisely, splitting at $IFS characters) and wildcard expansion (globbing) unless it's in double quotes (or a few other contexts that don't allow multiple words).

You can look up what exists in POSIX by reading the specification. Modern versions of POSIX are identical to the Open Group Base Specifications (without optional components). Older versions are a subset of Single Unix v2.

Unix-like systems without a POSIX shell are extremely rare nowadays. /bin/sh is a non-POSIX Bourne shell on a few systems, notably Solaris, but a POSIX shell is available (/usr/xpg4/bin/sh on Solaris, and you should have /usr/xpg4/bin ahead of /usr/bin in your PATH). If you need compatibility with Bourne shells, check the man page on the systems you're interested in, as there have been many versions of sh with slightly different sets of features. Sven Mascheck maintains a page with a lot of information.