Reputation: 177
In Shell script i am trying to pass an array to a Sudo SU command as shown below
VAR=(1,2,3)
sudo su $USER -c "for p in ${VAR[@]} ; do echo $p ; done;"
Issue is $p
value is not getting printed. I have tried all the below possible way but not able to print $p
value
sudo su $USER -c "for p in "${VAR[@]}" ; do echo "$p" ; done;"
sudo su $USER -c 'for p in ${VAR[@]} ; do echo $p ; done;'
Sample Output of the command
+ sudo su USER -c 'for p in 1 2 3 4 5 ; do echo ; done;'
+ echo "Script Ended"
Upvotes: 3
Views: 781
Reputation: 44102
You could use sh
as an argument to sudo su
, to pass the variable, then, inside the -c
call, you can access the var as you would do in a regular function call ($@ $1 ...
);
sudo su $USER -c 'for p in "${@}"; do echo ${p}; done;' sh "${arr[@]}"
so@deb:~$ arr=(1 2 3)
so@deb:~$ sudo su $USER -c 'for p in "${@}"; do echo ${p}; done;' sh "${arr[@]}"
1
2
3
so@deb:~$
Upvotes: 5