SC2298 – ShellCheck Wiki

See this page on GitHub

Sitemap


${$x} is invalid. For expansion, use ${x}. For indirection, use arrays, ${!x} or (for sh) eval.

(or ${${x}} is invalid)

Problematic code:

# Expecting $RETRIES or 3 if unset
retries=${$RETRIES:-3}

or

mypath="/tmp/foo.txt"
var=mypath
result=${$var##*/}  # Expecting ${mypath##*/}, i.e. 'foo.txt'

Correct code:

retries=${RETRIES:-3}

or

mypath="/tmp/foo.txt"
var=mypath
result=${!var}
result=${result##*/}

Rationale:

ShellCheck found a parameter expansion ${..} where the first element was a second parameter expansion, either ${$x..} or ${${x}..}. This is not valid.

In the first example, the extra $ was unintentional and should simply be deleted.

In the second example, ${$var##*/} was used in the hopes that it would expand to ${myvar##*/} and subsequently strip the path. This is not possible, and var must instead be expanded indirectly in a separate step, before the path can be stripped as usual. More information and other approaches can be found in the description of SC2082.

Exceptions:

None


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