Hari
Hari

Reputation: 5217

Evaluating variables from a list of names in bash

vars="a,b"
a="True"
b="False"
IFS=","
for var in $vars; do
  if [[ "$var" = "True" ]]; then
    echo "True found"    
  fi
done

I would expect the above bash script to print out "True found". But it does not print anything. Any ideas as to why ?

Upvotes: 0

Views: 36

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295403

Make this:

if [[ "${!var}" = "True" ]]; then

${!varname} expands the variable named in $varname. Otherwise, you get the name itself, not the contents of the variable with that name.

See BashFAQ #6 for far more details.

Upvotes: 2

Related Questions