Your issue is the quotes you're using. You need to use double quotes so that the variables within, $@
, can be expanded, otherwise they'll remain literals and never get expanded.
Here's an example
$ cat runme.bash
#!/bin/bash
echo "run with double quotes"
su -c "echo $@" user1
echo "run with single quotes"
su -c 'echo $@' user1
$ ./runme.bash "a b c"
run with double quotes
a b c
run with single quotes
$
You also have to pass the list of arguments in as a quoted list, otherwise the su -c ...
command gets confused and starts trying to parse the 2nd argument as the user that you want to su
as.
Another example
$ ./runme.bash a b c
run with double quotes
su: user b does not exist
run with single quotes
$
Debugging tip
If you want to see what the script is actually doing you can run it with the -x
switch to bash
.
bare arguments
$ bash -x ./runme.bash a b c
+ echo 'run with double quotes'
run with double quotes
+ su -c 'echo a' b c user1
su: user b does not exist
+ echo 'run with single quotes'
run with single quotes
+ su -c 'echo $@' user1
$
quoted arguments
$ bash -x ./runme.bash "a b c"
+ echo 'run with double quotes'
run with double quotes
+ su -c 'echo a b c' user1
a b c
+ echo 'run with single quotes'
run with single quotes
+ su -c 'echo $@' user1
$
$@
:su user -c 'command "$@"' -- argv0 "$@"
. (you should omit the--
on non-linux systems). See the answer here for an explanation.