Skip to content

Files

Latest commit

 

History

History
33 lines (21 loc) · 848 Bytes

SC2203.md

File metadata and controls

33 lines (21 loc) · 848 Bytes

Pattern: Use of glob in [[ ]]

Issue: -

Description

Globs in [[ ]] will not filename expand, and will be treated literally (or as patterns on the right-hand side of =, == and !=).

Example of incorrect code:

[[ current.log -nt backup/*.log ]] && echo "This is the latest file"

This is equivalent to [[ current.log -nt 'backup/*.png' ], and will look for a file with a literal asterisk in the name.

Instead, you can iterate over the filenames you want with a loop, and apply your condition to each filename.

Example of correct code:

newerThanAll=true
for log in backup/*.log
do
  [[ current.log -nt "$log" ]] || newerThanAll=false
done
[[ "$newerThanAll" = "true" ]] && echo "This is the latest file"

Further Reading