Shell – Portability of test, [, and [[ in Different Shells

portabilityposixshelltest

I see I can do

$ [ -w /home/durrantm ] && echo "writable"
writable

or

$ test -w /home/durrantm && echo "writable"
writable

or

$ [[ -w /home/durrantm ]] && echo "writable"
writable

I like using the third syntax. Are they equivalent in all ways and for all negative and edge cases? Are there any differences in portability, e.g. between bash on Ubuntu and on OS X or older/newer bash versions, e.g. before/after 4.0 and do they both expand expressions the same way?

Best Answer

[ is synonym of the test command and it is simultaneously a bash builtin and separate command. But [[ is a bash keyword and works in some versions only. So for reasons of portability you are better off using single [] or test

[ -w "/home/durrantm" ] && echo "writable"
Related Question