Pass arguments with spaces to command from shell script?


Adi Gerber

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" dddwill 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 $PARAMSis

--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 $PARAMSand paste its output , I get the expected output from it ./test. So I've tried removing the last line of execution and ./testleaving the last line on the last line, but that still doesn't work, echoapparently , 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!

Adi Gerber

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

Related


Pass arguments with spaces to command from shell script?

Adi Gerber 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" dddwill give the following output: aaa bbbb cccc ddd This is what

Shell script how to pass arguments with spaces in variables

silver glitter I'm making a script for synchronizing directories with rsync over ssh. I'm having trouble trying to define a custom port. Suppose a normal working script has the following syntax: #! /bin/sh rval=2222 port="ssh -p $rval" rsync --progress -av -

Shell script how to pass arguments with spaces in variables

silver glitter I'm making a script for synchronizing directories with rsync over ssh. I'm having trouble trying to define a custom port. Suppose a normal working script has the following syntax: #! /bin/sh rval=2222 port="ssh -p $rval" rsync --progress -av -

Shell script how to pass arguments with spaces in variables

silver glitter I'm making a script for synchronizing directories with rsync over ssh. I'm having trouble trying to define a custom port. Suppose a normal working script has the following syntax: #! /bin/sh rval=2222 port="ssh -p $rval" rsync --progress -av -

Shell script how to pass arguments with spaces in variables

silver glitter I'm making a script for synchronizing directories with rsync over ssh. I'm having trouble trying to define a custom port. Suppose a normal working script has the following syntax: #! /bin/sh rval=2222 port="ssh -p $rval" rsync --progress -av -

Shell script how to pass arguments with spaces in variables

silver glitter I'm making a script for synchronizing directories with rsync over ssh. I'm having trouble trying to define a custom port. Suppose a normal working script has the following syntax: #! /bin/sh rval=2222 port="ssh -p $rval" rsync --progress -av -

Pass command line arguments to awk in shell script

Thuro Background information: I'm trying to write a small shell script that searches a .fas file for a pattern (string) and prints the line and position where the pattern was found. The following snippet works when I call the shell script: Script (search.sh):

Pass command line arguments to awk in shell script

Thuro Background information: I'm trying to write a small shell script that searches a .fas file for a pattern (string) and prints the line and position where the pattern was found. The following snippet works when I call the shell script: Script (search.sh):

Pass command line arguments to awk in shell script

Thuro Background information: I'm trying to write a small shell script that searches a .fas file for a pattern (string) and prints the line and position where the pattern was found. The following snippet works when I call the shell script: Script (search.sh):

Pass arguments to command in bash shell script

Dhruv Jagetiya I am trying to use shell scripting to automate a small build process. The build process is basically: cd /location/to/build.xml /Path/To/ant version. Run ant with release as an argument. I created a build.sh file that I intend to use as a ./buil

How to pass command line arguments into shell script?

Paul I know that shell scripts just run commands as if they were executed at the command prompt. I'd like to be able to run a shell script like a script...that is, bring an input value or string into the script. How can I do this? Bruce Ediger The shell comman

Pass command line arguments to awk in shell script

Thuro Background information: I'm trying to write a small shell script that searches a .fas file for a pattern (string) and prints the line and position where the pattern was found. The following snippet works when I call the shell script: Script (search.sh):

How to pass command line arguments into shell script?

Paul I know that shell scripts just run commands as if they were executed at the command prompt. I'd like to be able to run a shell script like a script...that is, bring an input value or string into the script. How can I do this? Bruce Ediger The shell comman

Pass arguments to command in bash shell script

Dhruv Jagetiya I am trying to use shell scripting to automate a small build process. The build process is basically: cd /location/to/build.xml /Path/To/ant version. Run ant with release as an argument. I created a build.sh file that I intend to use as a ./buil

Pass arguments to Python from shell script

sk I wrote a small shell script like this: cd models/syntaxnet var1=$(jq --raw-output '.["avl_text"]' | syntaxnet/demo.sh) echo $var1 python /home/sree/python_code1.py $var1 Mine python_code1.pylooks like this: import sys data = sys.argv[1] print "In python

Pass arguments to shell script from Python

RRWW I am writing a python program that passes arguments to a shell script. Here is my python code: import subprocess Process=subprocess.Popen('./copyImage.sh %s' %s str(myPic.jpg)) and my "copyImage.sh": #!/bin/sh cp /home/pi/project/$1 /home/pi/project/ne

Pass arguments to Python from shell script

sk I wrote a small shell script like this: cd models/syntaxnet var1=$(jq --raw-output '.["avl_text"]' | syntaxnet/demo.sh) echo $var1 python /home/sree/python_code1.py $var1 Mine python_code1.pylooks like this: import sys data = sys.argv[1] print "In python

Pass arguments to Python from shell script

sk I wrote a small shell script like this: cd models/syntaxnet var1=$(jq --raw-output '.["avl_text"]' | syntaxnet/demo.sh) echo $var1 python /home/sree/python_code1.py $var1 Mine python_code1.pylooks like this: import sys data = sys.argv[1] print "In python

Pass arguments to Python from shell script

sk I wrote a small shell script like this: cd models/syntaxnet var1=$(jq --raw-output '.["avl_text"]' | syntaxnet/demo.sh) echo $var1 python /home/sree/python_code1.py $var1 Mine python_code1.pylooks like this: import sys data = sys.argv[1] print "In python

Pass arguments to Python from shell script

sk I wrote a small shell script like this: cd models/syntaxnet var1=$(jq --raw-output '.["avl_text"]' | syntaxnet/demo.sh) echo $var1 python /home/sree/python_code1.py $var1 Mine python_code1.pylooks like this: import sys data = sys.argv[1] print "In python

How to pass arguments with spaces from bash script to bash script?

Alik Elzin-kilaka I have a script that processes data, creates parameters and sends it to a second script. One of the parameters contains a space. script1.sh: args=() args+=("A") args+=("1 2") args+=("B") . script2.sh ${args[@]} script2.sh: for f in "$@" do