shell – Expanding variables in zsh

shellzsh

The script below works in bash but not in zsh. I think it is because in the variable OPTS, I am "expanding" (not sure if this is the right word) the variable $EXCLUDE, and this syntax doesn't work in zsh. How would I replace that line to make it work on zsh?

SRC="/path_to_source"
DST="/path_to_dest"
EXCLUDE=".hg"  
OPTS="-avr --delete --progress --exclude=${EXCLUDE} --delete-excluded"                                               

rsync $OPTS $SRC $DST   

Best Answer

The problem here is that $OPTS is not split into several arguments on the rsync command line. In zsh syntax, use:

rsync ${=OPTS} $SRC $DST 

(an alternative is to simulate standard shell behavior globably with the option -o shwordsplit…)

From the manpage:

One commonly encountered difference [in zsh] is that variables substituted onto the command line are not split into words. See the description of the shell option SH_WORD_SPLIT in the section 'Parameter Expansion' in zshexpn(1). In zsh, you can either explicitly request the splitting (e.g. ${=foo}) or use an array when you want a variable to expand to more than one word. See the section 'Array Parameters' in zshparam(1).

Related Question