BASH Wild-card: select all variables (not content)

I have a bunch of variables that I want to check, and if they contain a value of "No", I want to clear 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 all this works fine and dandy, only since I have a lot of varN, I will have a ton if [[ "${varN}" == "None" ]] and . I need to know their names; so I was wondering, since it in BASH is very similar to searching and matching everything, if there is a wild card for variables inside a for loop that will match all vars, something like ${*}(I tried this, it doesn’t work)? I did all kinds of searches, but always found something about matching the contents of a variable, not about var itself.?

+3
source share
3 answers

, . ( *).

$ 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
+6

compgen:

man bash | less -p 'compgen .option. .word.'
compgen -A variable B
+2

All Yes; -)

Most Unix / Linux support either env, or printenvthat produce output, for example

 var=value

A command exportwith no arguments lists all exported variables in your environment.

for varAndVal in $( env ) ; do
   case ${varAndVal} in
     *=none ) 
      eval \$${varAndVal}=
      #OR eval unset \$${varAndVal}
     ;;
    esac
 done

Hope this helps.

PS as you, it seems, a new user, if you get an answer that helps you remember to mark it as accepted and / or give it + (or -) as a useful answer.

+1
source

All Articles