I made two test bash scripts on Linux to make the problem clear.
TestScript1 looks like: echo "TestScript1 Arguments:"
echo "$1"
echo "$2"
echo "$#"
./testscript2 $1 $2
TestScript2 looks like:
echo "TestScript2 Arguments received from TestScript1:"
echo "$1"
echo "$2"
echo "$#"
When i execute testscript1 in the following way:
./testscript1 "Firstname Lastname" testmail@domain.com
The desired Output should be:
TestScript1 Arguments:
Firstname Lastname
testmail@domain.com
2
TestScript2 Arguments received from TestScript1:
Firstname Lastname
testmail@domain.com
2
But the actual output is:
TestScript1 Arguments:
Firstname Lastname
testmail@domain.com
2
TestScript2 Arguments received from TestScript1:
Firstname
Lastname
3
How do i solve this problem? I want to get the desired output instead of the actual output.
Quote your args in Testscript 1:
I found following program works for me
in test2.sh you use
$1
to refer variablea
in test1.shecho $1
The output would be
xxx
You need to use :
"$@"
(WITH the quotes) or"${@}"
(same, but also telling the shell where the variable name starts and ends).(and do NOT use :
$@
, or"$*"
, or$*
).ex:
I'm not sure I understood your other requirement ( you want to invoke './testscript2' in single quotes?) so here are 2 wild guesses (changing the last line above) :
Please give me the exact thing you are trying to do
edit: after his comment saying he attempts tesscript1 "$1" "$2" "$3" "$4" "$5" "$6" to run : salt 'remote host' cmd.run './testscript2 $1 $2 $3 $4 $5 $6'
You have many levels of intermediate: testscript1 on host 1, needs to run "salt", and give it a string launching "testscrit2" with arguments in quotes...
You could maybe "simplify" by having:
if THAt doesn't work, then instead of running "testscript2 ${theargs}", replace THE LAST LINE above by