Reputation:
I am creating a detached screen, player, that runs the script, player.sh, which has multiple arguments from AR, array.
AR=('foo' 'bar' 'space' 'bat')
screen -S player -dm bash -c "/home/deanresin/scripts/player.sh ${AR[@]}"
but it gets parsed as...
+ screen -S player -dm bash -c '/home/deanresin/scripts/player.sh foo' bar space bat
and only the first argument, foo, is passed to player.sh.
If I manually enter the array it works..
screen -S player -dm bash -c "/home/deanresin/scripts/player.sh foo bar space bat"
+ screen -S player -dm bash -c '/home/deanresin/scripts/player.sh foo bar space bat'
and they all get passed to player.sh.
Please help, I've been pulling my hair out.
edit:
I also tried..
screen -S player -dm bash -c "/home/deanresin/scripts/player.sh "`echo "${AR[@]}"`
with the same unwanted result.
Upvotes: 0
Views: 1973
Reputation: 123470
It's easier if you don't try to add the indirection via bash -c
:
screen -S player -dm /home/deanresin/scripts/player.sh "${AR[@]}"
If you ever do need to nest it in a bash -c
, you should escape the arguments properly and concatenate them into that single argument:
screen -S player -dm bash -c "/home/deanresin/scripts/player.sh $(printf "%q " "${AR[@]}")"
or pass them as separate arguments that become available as $@
in the script string itself, which much be carefully quoted to ensure it's literal:
screen -S player -dm bash -c '/home/deanresin/scripts/player.sh "$@"' _ "${AR[@]}"
The wrong solution is using bash -c "/home/deanresin/scripts/player.sh ${AR[*]}"
instead to concatenate all the elements without escaping, but this doesn't account for shell metacharacters and is therefore best avoided.
Upvotes: 2