0

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
deanresin
  • 1,466
  • 2
  • 16
  • 31

1 Answers1

3

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.

that other guy
  • 116,971
  • 11
  • 170
  • 194
  • I tried your preferred first suggestion and it worked. Though I'm confused what `bash -c` indirection is for. I thought you needed it to state you were sending a bash command. I'm ignorant as to what it is really for. – deanresin Dec 04 '18 at 05:17