Created
November 10, 2010 16:24
-
-
Save l0b0/671067 to your computer and use it in GitHub Desktop.
test_bash_set.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
echo ' ${var-_} ${var:-_} $var ${!var[@]}' | |
test_all() | |
{ | |
test "${var-_}" && echo -n 'T ' || echo -n 'F ' | |
test "${var:-_}" && echo -n 'T ' || echo -n 'F ' | |
test "$var" && echo -n 'T ' || echo -n 'F ' | |
[[ "${!var[@]}" ]] && echo 'T' || echo 'F' | |
} | |
declare var | |
echo -n 'unset ' | |
test_all | |
var='' | |
echo -n 'empty ' | |
test_all | |
var=' ' | |
echo -n 'non-empty ' | |
test_all | |
var=(var1 var2 var3) | |
echo -n 'array ' | |
test_all |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In response to http://stackoverflow.com/questions/874389/bash-test-for-a-variable-unset-using-a-function/1469472#1469472