fabic974 random (binary) wanderings in the philament empires…

Bash: Test if variable is set (exists) or not

Use Bash substitution ${var+x} for testing whether or not a variable (incl. environment one) is set or not (via).

[ -z ${EDITOR+x} ] && echo "\$EDITOR IS NOT SET" || echo "SET: $EDITOR" ]

Example :

[ -z ${CC+x}  ] && type -p clang >/dev/null
  && CC=clang
  && echo "| \$CC was set to $CC"

Likewise for CXX :

[ -z ${CXX+x} ] && type -p clang++ >/dev/null
  && CXX=clang++
  && echo "| \$CXX was set to $CXX"

EOF