Skip to content

Files

Latest commit

 

History

History
53 lines (38 loc) · 822 Bytes

SC2149.md

File metadata and controls

53 lines (38 loc) · 822 Bytes

Pattern: Unnecessary $/${} for numeric index

Issue: -

Description

For a numerically indexed array, the $ is mostly pointless and can be removed.

For associative arrays, the $ should be escaped to avoid accidental dereferencing:

declare -A array
index='$1'
array[$index]=42
echo "$(( array[$index] ))"    # bash: array: bad array subscript
echo "$(( array[\$index] ))"   # 42

Example of incorrect code:

# Regular array
index=42
echo $((array[$index]))

or

# Associative array
index=banana
echo $((array[$index]))

Example of correct code:

# Regular array
index=42
echo $((array[index]))

or

# Associative array
index=banana
echo $((array[\$index]))

Further Reading