In the example below, why does $var get passed to the su in the start function, but not the array modules ?#!/bin/bashvar=catmodules=(onetwothreefour)start() {    su gleventh -c for i in ${modules[@]}; do echo -- $i -- $var;  done}$1echo out of functionfor i in ${modules[@]}; do echo -- $i -- $var;  doneThe script above returns:$ sudo ./test.sh start-- -- cat-- -- cat-- -- cat-- -- catout of function-- one -- cat-- two -- cat-- three -- cat-- four -- cat
Why is a variable passed to the su command, but not an array from the same scope?
bash;shell;quoting
The problem was that since the su statement was in double quotes, the variables were all expanded before the su command was called, which means $var becomes cat but $i becomes  since it was not defined.  Bash doesn't know that it was supposed to be an iterator variable, it just expands it to a NULL string.  The answer is to escape that $ like so:#!/bin/bashvar=catmodules=(onetwothreefour)start() {    su gleventh -c for i in ${modules[@]}; do echo -- \$i -- $var;  done}$1echo out of functionfor i in ${modules[@]}; do echo -- $i -- $var;  done