10 SC1037
John Gardner edited this page 2021-12-22 19:31:44 +11:00

Braces are required for positionals over 9, e.g. ${10}.

Problematic code:

echo "Ninth parameter: $9"
echo "Tenth parameter: $10"

Correct code:

echo "Ninth parameter: $9"
echo "Tenth parameter: ${10}"

Rationale:

For legacy reasons, $10 is interpreted as the variable $1 followed by the literal string 0.

Curly braces are needed to tell the shell that both digits are part of the parameter expansion.

Exceptions

If you wanted the trailing digits to be literal, ${1}0 will make this clear to both humans and ShellCheck.

In dash, $10 is (wrongly) interpreted as ${10}, so some 'reversed' care should also be taken:

bash -c 'set a b c d e f g h i j; echo $10 ${1}0' # POSIX: a0 a0
dash -c 'set a b c d e f g h i j; echo $10 ${1}0' # WRONG: j a0