0

If I have a script:

#!/bin/bash
for ARGUMENT in "$@"
do
    echo $ARGUMENT
done

and run

./test.sh "hello there" my name is

I get expected output:

hello there
my
name
is

Yet I'm trying to pass arguments from another script, and want to run the command:

COMMAND="./test.sh \"hello there\" my name is"

echo $COMMAND I get what's expected:

./test.sh "hello there" my name is

HOWEVER running $COMMAND doesn't seem to respect the escaped quotes:

$COMMAND

gives output:

"hello
there"
my
name
is

"hello and there" are seen as separate args...

I am probably not escaping something correctly. I expect the first output above, but I'm getting quotes in my output rather them being evaluated as a way to group the bash arguments. What am I doing wrong?

Doug T.
  • 64,223
  • 27
  • 138
  • 202
  • 2
    This is a classic case of [How to store a command in a variable in a shell script?](https://stackoverflow.com/q/5615717/5291015) and what to do in those cases. – Inian May 06 '20 at 17:43
  • 2
    Store the constructs in an array i.e. `cmd=("./test.sh" "hello there" my name is)` and a quoted expansion `"${cmd[@]}"`. Assuming `test.sh` is made an executable – Inian May 06 '20 at 17:45

0 Answers0