Testing VARs

Published: Wednesday, Dec 26, 2007 Last modified: Thursday, Nov 14, 2024

I’ve noticed in some shell scripts these sorts of tests:

if [ x$SSH_AUTH_SOCK != x ]; then

So if $SSH_AUTH_SOCK isn’t set, it won’t enter the if.

In Bash there are other ways to test a var exists:

[ ${SSH_AUTH_SOCK:+set} ]

or

[[ $SSH_AUTH_SOCK ]]

Though these account for WHITESPACE they do not work in every shell.

Thanks to twkm on #bash for taking the time to explain this to me.