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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s