An Example Showing the Difference Between [ and [[ in shell (bash) on pathname Expansion

How to understand “No filename expansion or word splitting takes place between [[ and ]], but there is parameter expansion and command substitution.” [1], especially the “no filename expansion” part?

“no filename expansion” means that wildcards such as “*”, “?” and “[” will not be treated specially. That is, a string like “/dev/stder?” will not be expanded to match string patterns which is started with “/dev/stder” and has one character we are not certain of.

A simple example showing the difference between [ and [[ for filename expansion is as follows.

if [[ -e /dev/stder? ]]; then echo "matched"; else echo "wrong"; fi

will return

wrong

whereas

if [ -e /dev/stder? ]; then echo "matched"; else echo "wrong"; fi

will yield

matched

 

[1] Cooper, M. (2014). Advanced Bash Scripting Guide. Рипол Классик.

Leave a comment