Skip to content

Latest commit

 

History

History
36 lines (24 loc) · 877 Bytes

SC2199.md

File metadata and controls

36 lines (24 loc) · 877 Bytes

Pattern: Array expansion in [[ ]]

Issue: -

Description

Array expansions in [[ .. ]] will implicitly concatenate into a single string, much like in assignments.

Example of incorrect code:

ext=png
allowedExt=(jpg bmp png)
[[ "$ext" = "${allowedExt[@]}" ]] && echo "Extension is valid"

This is equivalent to [ "$ext" = "jpg bmp png" ].

Instead, use a for loop to iterate over values, and apply your condition to each.

Alternatively, if you do want to concatenate all the values in the array into a single string for your test, use "$*" or "${array[*]}" to make this explicit.

Example of correct code:

ext=png
allowedExt=(jpg bmp png)
for value in "${allowedExt[@]}"
do
  [[ "$ext" = "$value" ]] && echo "Extension is valid"
done

Further Reading