Reputation: 8367
I have a bunch of variables that I want to check, and if they contain the value "None" then I want to empty them.
var1=(check for some value, sometimes it returns "none")
var2=(check for some value, sometimes it returns "none")
var3=(check for some value, sometimes it returns "none")
someBizzareName=(check for some value, sometimes it returns "none")
if [[ "${var1}" == "None" ]] ; then
var1=""
fi
if [[ "${var2}" == "None" ]] ; then
var2=""
fi
And this is all working fine and dandy, only since I have a lot of varN, I will end up with a ton of if [[ "${varN}" == "None" ]]
and I have to know their names ; so I was wondering, since it's in BASH very nature to search and match everything, if there is a wild-card for variables, inside a for loop, that will match all vars, something like ${*}
(I tried that, does'nt work) ? I have done all kinds of searches but always find something about matching variable content, not the var itself..?
Upvotes: 5
Views: 4486
Reputation: 21
You may also use the builtin compgen:
man bash | less -p 'compgen .option. .word.'
compgen -A variable B
Upvotes: 2
Reputation: 37288
All, yes ;-)
Most Unix/Linux support either the env
or printenv
which produce output like
var=value
The export
command, without arguments, will list all exported variables in your environment.
for varAndVal in $( env ) ; do
case ${varAndVal} in
*=none )
eval \$${varAndVal}=
#OR eval unset \$${varAndVal}
;;
esac
done
I hope this helps.
P.S. as you appear to be a new user, if you get an answer that helps you please remember to mark it as accepted, and/or give it a + (or -) as a useful answer.
Upvotes: 1
Reputation: 798744
All, no. But you can match a pattern (but not *
).
$ echo "${!B*}"
BASH BASHOPTS BASHPID BASH_ALIASES BASH_ARGC BASH_ARGV BASH_CMDS BASH_COMMAND BASH_LINENO BASH_SOURCE BASH_SUBSHELL BASH_VERSINFO BASH_VERSION
Upvotes: 8