Bash string concatenation used to build parameter list
Solution 1
There is a difference between:
PARMS+="... --exclude='.git'"
and
... --exclude='.git'
In the first, the single quotes are inside quotes themselves, so they are literally present in the substituted text given to rsync
as arguments. rsync
gets an argument whose value is --exclude='.git'
. In the second, the single quotes are interpreted by the shell at the time they're written, because they aren't inside quotes themselves, and rsync
gets to see --exclude=.git
.
In this case, you don't need the single quotes at all — .git
is a perfectly valid shell word on its own, with no special characters, so you can use it literally in the command.
Better for this kind of thing, though, is an array:
PARMS=(-rvu)
PARMS+=(--delete --exclude='.git')
rsync "${PARMS[@]}"
This builds up your command as separate words, with whatever quoting you want interpreted at the time you write the array line. "${PARMS[@]}"
expands to each entry in the array as a separate argument, even if the argument itself has special characters or spaces in it, so rsync
sees what you wrote as you meant it.
Solution 2
In addition to @Michael Homer's answer, you can use bash
eval function:
PARMS='-rvu'
PARMS+=" --delete --exclude='.git'"
echo "$PARMS"
eval "rsync ${PARMS} . "'"${TARGET}"'
Related videos on Youtube
neuviemeporte
Updated on September 18, 2022Comments
-
neuviemeporte almost 2 years
Given this piece of bash:
PARMS='-rvu' PARMS+=" --delete --exclude='.git'" echo $PARMS rsync ${PARMS} . ${TARGET}
The echo shows the PARMS string as expected, no error is displayed, but rsync silently acts as if the options added by the += did not exist. However, this works as expected:
PARMS='-rvu' rsync ${PARMS} --delete --exclude='.git' . ${TARGET}
I guess I screwed something up with bash quotes (always had problems with those), but not exactly sure what and why are the options ignored even though the string seems to have been built correctly.
-
jasonwryan almost 10 years
echo "$PARMS"
andrsync "${PARMS}"
... -
Anthon almost 10 yearsThis works for me with
bash
version 4.2.25 without any changes.
-
-
cuonglm almost 10 years
bash
performed word spliting after${PARMS}
was expanded. So the single quote was also interpreted by the shell. -
Michael Homer almost 10 yearsTry it! I did. The quotes remain, and if there were spaces in between they are split points anyway.
-
camh almost 10 years@Gnouc: From the bash man page: "Quote Removal: After the preceding expansions, all unquoted occurrences of the characters
\
,'
, and"
that did not result from one of the above expansions are removed." "above expansions" includes Parameter Expansion which performs the expansion of${PARMS}
. -
neuviemeporte almost 10 yearsThanks. So I understand that in that case omitting the single quotes inside the double ones will work but for completeness' sake - what if I needed to quote some special characters and didn't want to use your latter approach?
-
Michael Homer almost 10 yearsIf your special characters are not part of
IFS
(generally, whitespace), you don't need to quote them. If they are, you're out of luck unless you hack something together witheval
— this is a bit of a misfeature in general, and arrays are the right way to deal with it. -
chepner almost 10 yearsYou can, but you shouldn't. Arrays were added specifically to avoid this use of
eval
.