mercredi 4 mars 2015

Why is a variable passed to the su command, but not an array from the same scope?


In the example below, why does $var get passed to the su in the start function, but not the array modules ?



#!/bin/bash
var=cat
modules=(
one
two
three
four
)

start() {
su gleventh -c "for i in ${modules[@]}; do echo -- $i -- $var; done"
}

$1

echo "out of function"
for i in ${modules[@]}; do echo -- $i -- $var; done


The script above returns:



$ sudo ./test.sh start
-- -- cat
-- -- cat
-- -- cat
-- -- cat
out of function
-- one -- cat
-- two -- cat
-- three -- cat
-- four -- cat


Aucun commentaire:

Enregistrer un commentaire