SC3053 – ShellCheck Wiki

See this page on GitHub

Sitemap


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.


ShellCheck is a static analysis tool for shell scripts. This page is part of its documentation.