0

Similar thread:

How can I escape a double quote inside double quotes?

I have a freebsd box with GNU bash, version 4.4.0(0)-release.

On this box there is a shell script running rsync, this script needs to specify an rsync path parameter which has space inside it.

dqt='"'

RSYNC="/usr/local/bin/rsync -avzn --rsync-path=${dqt}sudo rsync${dqt}"

The interpreter is bash but just to make sure I have tried it with specifically bash -x and here is what I get:

+ /usr/local/bin/rsync -avzn '--rsync-path="sudo' 'rsync"' --numeric-ids --delete ...

Which of course going to lead to rsync error: syntax or usage error. Any ideas how to fix this?

Run rsync fine as:

rsync-path="sudo rsync"

nft
  • 3
  • 2

2 Answers2

0

You have to change the quoting from double quoted to single quoted, like:

RSYNC="/usr/local/bin/rsync -avzn --rsync-path="'"sudo rsync"'

I've used this in a small bash script:

#!/bin/bash -x

RSYNC="/usr/local/bin/rsync -avzn --rsync-path="'"sudo rsync"'
echo "#" $RSYNC "#"

eval "$RSYNC"

with the result:

+ RSYNC='/usr/local/bin/rsync -avzn --rsync-path="sudo rsync"'
+ echo '#' /usr/local/bin/rsync -avzn '--rsync-path="sudo' 'rsync"' '#'
# /usr/local/bin/rsync -avzn --rsync-path="sudo rsync" #
+ eval '/usr/local/bin/rsync -avzn --rsync-path="sudo rsync"'
++ /usr/local/bin/rsync -avzn '--rsync-path=sudo rsync'
./x: line 6: /usr/local/bin/rsync: No such file or directory

OK, I don't have an rsync in /usr/local bin, but you can see that the last argument, --rsync-path=sudo rsync is enclosed in single quotes and thus a single arument,

Ronald
  • 2,842
  • 16
  • 16
  • This is not even close. It will produce '--rsync-path="sudo' 'rsync"' I have messed around with these "' combinations already – nft Apr 01 '22 at 09:42
  • No, regardless your suggestion this does not work. I get: /usr/local/bin/rsync -avzn '--rsync-path="sudo' 'rsync"' --numeric-ids – nft Apr 01 '22 at 10:47
0

Your rsync command seems to have been word-split. You can't store a full command in a variable, you have to use an array:

RSYNC_ARR=( /usr/local/bin/rsync -avzn --rsync-path="sudo rsync" )

"${RSYNC_ARR[@]}" # the double quotes are mandatory here

A little test to see the difference:

#!/bin/bash

RSYNC='/usr/local/bin/rsync -avzn --rsync-path="sudo rsync"'

RSYNC_ARR=( /usr/local/bin/rsync -avzn --rsync-path=sudo\ rsync )

set -x

: $RSYNC
#+ : /usr/local/bin/rsync -avzn '--rsync-path="sudo' 'rsync"'

: ${RSYNC_ARR[@]}
#+ : /usr/local/bin/rsync -avzn --rsync-path=sudo rsync

: "$RSYNC"
#+ : '/usr/local/bin/rsync -avzn --rsync-path="sudo rsync"'

: "${RSYNC_ARR[@]}"
#+ : /usr/local/bin/rsync -avzn '--rsync-path=sudo rsync'
Fravadona
  • 13,917
  • 1
  • 23
  • 35
  • This is still not good, this will produce: /usr/local/bin/rsync -avzn --rsync-path=sudo rsync --numeric-ids so as you see the " missing again and it will break – nft Apr 01 '22 at 09:45
  • That printf will output /usr/local/bin/rsync -avzn --rsync-path=sudo\ rsync which is still not good, breaks the script – nft Apr 01 '22 at 10:51
  • If it breaks then that means that you're using your `$RSYNC` variable in a context that I cannot guess. WHERE and HOW are you using your `$RSYNC` variable? – Fravadona Apr 01 '22 at 11:04
  • This worked the RSYNC_ARR and using, quoted "${RSYNC_ARR[@]}" afterwards, thanks. – nft Apr 01 '22 at 14:17