Neoon Neoon - 5 months ago 11
Bash Question

Passing params to another Bash file, with spaces

I want to pass several parameters from one Bash file to another, the problem is, it seems to ignore my quote marks, I dont want to pass it with

$@
.

Here is my current setup:

agent.sh
:

#!/bin/bash

KEY='INSERT_KEY_HERE';

RESP='command start Test "srcds_run -game garrysmod +maxplayers 32 +map gm_construct"'

./CMD.sh ${KEY} ${RESP}


Actually the
RESP
is coming from a CURL but that gives me the same result.

CMD.sh
:

#!/bin/bash

echo $1;
echo $2;
echo $3;
echo $4;
echo $5;


And guess,
$5
gives me:

"srcds_run


instead of

srcds_run -game garrysmod +maxplayers 32 +map gm_construct


what I actually wanted.

So when using
$*
it gives me all parameters, fine but I want them splitted, same as using
$@
, I do overwrite the parameters at the angent.sh script but that turns all parameters into
$2
and I want them splitted...

So any solution to pass Parameters with Spaces without issues?

Answer

In this case you have to use an array (and more quotes):

#!/bin/bash

key='INSERT_KEY_HERE'

resp=( command start Test "srcds_run -game garrysmod +maxplayers 32 +map gm_construct" )

./CMD.sh "$key" "${resp[@]}"

Note that I used lowercase variable names (which is good practice!).