parsed.org

Tips by tag: iteration

Variable Indirection by xinu on Jan 31, 2006 10:06 AM

Sometimes you need to iterate over variable names and access their values. In shell script, you would do something like this to get the values of FOO and BAR:

$ FOO=apple
$ BAR=orange
$ VARS="FOO BAR"
$ for v in $VARS ; do echo ${!v} ; done

The above works in bash. Use this in Zsh:

$ for v in $VARS ; do echo ${(P)v} ; done

(The 'P' flag on ${v} causes a further variable lookup before ${v} is evaluated.)

Thanks to Cliff for the tip!

bashcommandsenvironmentevaluationiterationloopshellzsh
RSS