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 -e "$port" [email protected]:/home/sflash/Documents/tmp/tcopy/ /home/sflash/Documents/tmp/tcopy

The syntax when exposing a custom port is -e "ssh -p 2222". However, if I want to use variables in this case like:

#! /bin/sh

rval=2222
port="-e \"ssh -p $rval\""

rsync --progress -av $port [email protected]:/home/sflash/Documents/tmp/tcopy/ /home/sflash/Documents/tmp/tcopy

This probably won't work due to some form of interaction with IFS. This could be avoided entirely if I introduced an if statement to check if portit's defined, but I'm curious as to the exact reason why it fails and if there is a solution to enforce this.

EDIT: Sorry, I'm limited to the posix shell

William Purcell

You haven't actually provided enough detail to be sure, but I suspect you've run into a common misconception.

When you do this:

rval=2222
rsync --progress -av -e "ssh -p $rval" src dst

rsyncThe call requires 6 parameters: --progress, -av, -e, ssh -p 2222, src, and dst.

On the other hand, when you do:

port="-e \"ssh -p $rval\""
rsync --progress -av $port src dst

rsyncCalled with 8 arguments: --progress, -av, -e, "ssh, -p, 2222", src, and dst.

You don't want to pass double quotes to rsyncand you don't want to ssh -p 2222split it into 3 arguments. One (bad) way to do what you want is to use eval. But it looks like what you really want is:

rval=2222
port="ssh -p $rval"
rsync --progress -av ${port:+-e "$port"} src dst

Now, if port is defined instead of an empty string, it will be called rsyncwith additional arguments -eand ssh -p 2222(as needed) , and if port is undefined or empty, neither is used -enor specified $port.

Note that you cannot use double quotes around in this case ${port:+-e "$port"}. If you do this, when the empty string is an empty string, the empty string will be passed as a parameter $port. When $portnot the empty string, it will be passed a single argument -e ssh -p 2222instead of splitting into two arguments -eand ssh -p 2222.

Related


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 arguments containing spaces through shell variables

Markus Mayr I have a program that I want to call by passing arguments from shell variables. Throughout the question, I will assume that it is determined by #!/bin/sh echo $# That is, it prints out the number of arguments passed to it. call it count-args. I ca

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

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

How to pass arguments with spaces through environment variables?

Chaike On the bash shell, I want to pass arguments via environment variables. like this... $ export DOCKER_OPTIONS="-p 9200:9200 -e ES_JAVA_OPTS='-Xmx1g -Xms1g' -d " $ docker run -d $DOCKER_OPTIONS elasticsearch I want to pass "ES_JAVA_OPTS='-Xmx1g -Xms1g'" a

How to pass arguments with spaces through environment variables?

Chaike On the bash shell, I want to pass arguments via environment variables. like this... $ export DOCKER_OPTIONS="-p 9200:9200 -e ES_JAVA_OPTS='-Xmx1g -Xms1g' -d " $ docker run -d $DOCKER_OPTIONS elasticsearch I want to pass "ES_JAVA_OPTS='-Xmx1g -Xms1g'" a

How to pass spaces in arguments to commands in variables

Frederick I want to put the bash command to be executed in a variable and then execute that variable. The command has parameters, some of which may contain spaces. How can I do this? What I've tried so far is this: nbargs(){ echo $#; } # define some f

How to pass arguments with spaces through environment variables?

Chaike On the bash shell, I want to pass arguments via environment variables. like this... $ export DOCKER_OPTIONS="-p 9200:9200 -e ES_JAVA_OPTS='-Xmx1g -Xms1g' -d " $ docker run -d $DOCKER_OPTIONS elasticsearch I want to pass "ES_JAVA_OPTS='-Xmx1g -Xms1g'" a

How to pass arguments with spaces through environment variables?

Chaike On the bash shell, I want to pass arguments via environment variables. like this... $ export DOCKER_OPTIONS="-p 9200:9200 -e ES_JAVA_OPTS='-Xmx1g -Xms1g' -d " $ docker run -d $DOCKER_OPTIONS elasticsearch I want to pass "ES_JAVA_OPTS='-Xmx1g -Xms1g'" a

How to pass spaces in arguments to commands in variables

Frederick I want to put the bash command to be executed in a variable and then execute that variable. The command has parameters, some of which may contain spaces. How can I do this? What I've tried so far is this: nbargs(){ echo $#; } # define some f

How to pass shell script variables to Expect script?

User 2932003 I want to pass the $SPACE variable into the Expect script and get the output and then again into the Shell script. #!/bin/bash -x SPACE=$(df -h | awk '{ print $5 }' | grep -v Use |sort -n |tail -1 | cut -d % -f1) #set user "root" #set

How to pass arguments to file in shell script?

loss frequency Is it possible to pass arguments from shell script and pass it to file. This is what I basically want to achieve: my script: #!/bin/bash - input=$1 DIR=/home/user/ File=$DIR/test.txt `cat $File` < $input Here is test.txt: select * from abc

How to pass arguments to file in shell script?

loss frequency Is it possible to pass arguments from shell script and pass it to file. This is what I basically want to achieve: my script: #!/bin/bash - input=$1 DIR=/home/user/ File=$DIR/test.txt `cat $File` < $input Here is test.txt: select * from abc

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

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 shell script

A generation A very simple problem might be but can't get it to work. ...basically, I have an XML file, and internally, for unavoidable reasons, I choose a shell script that allows me to pass a small number of arguments to an external program. Now it works mos

Pass arguments to shell script

Keerthisairam I want to execute a shell script used in a usage()script. I have to provide three parameters in the usage block like this: ./script_name server_name path flag How can I write code blocks? Finally, I need to read all three parameters in one line

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

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

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

Shell script passing arguments with spaces

Christopher 2007 I have the following script (example): #!/bin/bash while getopts a: opt; do case "$opt" in a) val="$OPTARG";; ?) echo "use the flag \"-a\"" exit 2;; esac done echo "a specified with: ${val}" When I call th