0

In my bash script, in several places I need to run the command

rsync [stuff] --exclude={"/mnt/*","/proc/*"} [source] [destination]

To avoid typing out the whole list, I want to pack the option --exclude={"/mnt/*","/proc/*"} into a variable called EXCLUDES such that I can type in my script:

rsync [stuff] "$EXCLUDES" [source] [destination]

What is the proper way to achieve this?

3
  • Possible duplicate of Bash pass variable as argument with quotes Commented Nov 5, 2016 at 14:41
  • EXCLUDES='--exclude={"/mnt/*","/proc/*"}' Commented Nov 5, 2016 at 14:41
  • There is no problem: $EXCLUDES can expand into something which contains quotation marks; these are not processed in relation to the quotes around "$EXCLUDES". The $VAR syntax cannot perpetrate a "close quote injection attack" against the surrounding syntax. Commented Nov 5, 2016 at 14:46

1 Answer 1

2

Use an array:

EXCLUDES=(
  --exclude="/mnt/*"
  --exclude="/proc/*"
)
rsync [stuff] "${EXCLUDES[@]}" [source] [destination]

or a here-doc with the --exclude-from option:

rsync [stuff] --exclude-from - [source] [destination] <<EOF
/mnt/proc/*
/proc/*
EOF

I would recommend against using brace expansion in a script. Your text editor should make it easy to quickly duplicate repeated strings, and the the resulting script will be more readable. Brace expansions are intended for interactive use.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.