Pass arguments with spaces to command from shell script?
See edit below, thanks
I have the following test script ( IMPORTANT: I cannot change this part ):
while (($#)); do
echo $1
shift
done
Running the command ./test aaa "bbbb cccc" ddd
will give the following output:
aaa
bbbb cccc
ddd
This is what any sane person would expect.
I have another script:
mode="good"
status="okay"
name="bro"
description="very good man dood"
extra=""
# ----
PARAMS=""
// $1: key, $2: value
function addParam {
if [ ! -z "$2" ]; then
PARAMS="$PARAMS --$1 \"$2\""
fi
}
addParam "mode" "$mode"
addParam "status" "$status"
addParam "name" "$name"
addParam "description" "$description"
addParam "extra" "$extra"
echo ./test $PARAMS
./test $PARAMS
from the output echo
, ./test --mode "good" --status "okay" --name "bro" --description "very good man dood"
so my expected output ./test $PARAMS
is
--mode
good
--status
okay
--name
bro
--description
very good man dood
But for some reason I get the following output:
--mode
"good"
--status
"okay"
--name
"bro"
--description
"very
good
man
dood"
If I copy echo ./test $PARAMS
and paste its output , I get the expected output from it ./test
. So I've tried removing the last line of execution and ./test
leaving the last line on the last line, but that still doesn't work, echo
apparently , and more than I thought.$(./script)
What am I doing wrong?
EDIT: @steeldriver's solution works, but has another drawback - I have to allow users to send their own parameters.
So having this script (thanks @steeldriver):
#!/bin/bash
mode="good"
status="okay"
name="bro"
description="very good man dood"
extra=""
arguments="--config \"blablabla=yes\" --config2 \"bla2=no problem\""
# ----
declare -a PARAMS
# $1: key, $2: value
function addParam {
if [ ! -z "$2" ]; then
PARAMS+=("--$1" "$2")
fi
}
addParam "mode" "$mode"
addParam "status" "$status"
addParam "name" "$name"
addParam "description" "$description"
addParam "extra" "$extra"
# (1)
PARAMS+=("$arguments")
# (2)
PARAMS+=($arguments)
echo ./test "${PARAMS[@]}"
./test "${PARAMS[@]}"
The desired output is:
--mode
good
--status
okay
--name
bro
--description
very good man dood
--config
blablabla=yes
--config2
bla2=no problem
But the output I get is:
with (1)
:
--mode
good
--status
okay
--name
bro
--description
very good man dood
--config "blablabla=yes" --config2 "bla2=no problem"
with (2)
:
--mode
good
--status
okay
--name
bro
--description
very good man dood
--config
"blablabla=yes"
--config2
"bla2=no
problem"
Very grateful!
Thanks to @steeldriver's attention, I managed to add the user parameter correctly and get the desired output. Now my script:
#!/bin/bash
mode="good"
status="okay"
name="bro"
description="very good man dood"
extra=""
arguments="--config \"blablabla=yes\" --config2 \"bla2=no problem\""
# ----
declare -a PARAMS
# $1: key, $2: value
function addParam {
if [ ! -z "$2" ]; then
PARAMS+=("--$1" "$2")
fi
}
# This function right here
function addUserArguments {
while (($#)); do
PARAMS+=("$1")
shift
done
}
addParam "mode" "$mode"
addParam "status" "$status"
addParam "name" "$name"
addParam "description" "$description"
addParam "extra" "$extra"
# And this line right here
eval addUserArguments $arguments
./test "${PARAMS[@]}"
the output is
--mode
good
--status
okay
--name
bro
--description
very good man dood
--config
blablabla=yes
--config2
bla2=no problem