2 SC3053
Vidar Holen edited this page 2020-09-01 17:34:25 -07:00

In POSIX sh, indirect expansion is undefined.

(or "In dash, ... is not supported." when using dash)

Problematic code:

#!/bin/sh
name="PATH"
echo "${!name}"

Correct code:

The easiest solution is to switch to a shell that does support indirect expansion, like bash:

#!/bin/bash
name="PATH"
echo "${!name}"

Alternatively, carefully rewrite using eval:

#!/bin/sh
name=PATH
eval "echo \"\$$name\""

Rationale:

Indirection expansion is an extension in bash and ksh, and not supported in dash or POSIX sh. Either switch to a shell that supports them, or write around it with careful use of eval. Take care to validate the variable name to avoid fragility and code injection.

Exceptions:

If you only intend to target shells that supports this feature, you can change the shebang to a shell that guarantees support, or ignore this warning.

You can use # shellcheck disable=SC3000-SC4000 to ignore all such compatibility warnings.

  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!