I've got some options for commands stored in variable. Unfortunately whenever there is a space bash treats that as variable separator despite string being in quotes.
I am aware, that proper way of doing it is using arrays in bash, but this situation I've encountered is based on legacy scripts which are deploying code on commit in git. Unfortunately I have to stay with string-in-variable solution, which was doing fine till there was a need to use space in one of arguments (take a look on example, there is --filter='- /logs/'
parameter.
Example script:
#!/bin/bash
RSYNC_OPTS="-r --delete --exclude .env --filter='- /logs/' --links"
mkdir a b
rsync $RSYNC_OPTS a b
I got following error:
Unknown filter rule: `'-' rsync error: syntax or usage error (code 1) at exclude.c(927) [client=3.1.3]
As far as I understand, space after --filter='-
acted like argument separator despite being used in single quotes.
Until now I tried:
- quote mode for variable:
${RSYNC_OPTS@Q}
- Escaping space or single quotes in
RSYNC_OPTS
variable - Changing single quotes with double ones
How to make bash interpret string in quote properly when sending it as string in variable? I hope that there will be solution that does not involve changing RSYNC_OPTS to something else than single string text variable.