May 212012

Last night I looked at a few bash scripts that were a bit ugly. Each of them did more or less the same just used a different array. So I ended up merging them. The little issue I run into was that the used array name was passed as an argument to the script.

The solution was fairly easy by using indirect shell expansion, the important bit is the “!” in front of the variable c.

Below a simple example:


a1=(a b c); 
a2=(x y z); 

while getopts "p:" OPT; do
    case $OPT in 
        p) parameter=${OPTARG};;


for i in ${!c}; do
     echo ${i};