Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 05-15-2015, 10:08 AM   #1
LQ Newbie
Registered: Jul 2013
Posts: 11

Rep: Reputation: Disabled
Bash string substitution (rsync exclude)

I'm preparing a rsync script to backup from one machine to another through ssh. For this I'm successful with the following:


rsync -avzP --exclude={cristiano,marcelo} root@$IP:$ORIGIN $DESTINATION/

I would like to improve it, moving the exclude part to a variable also. I've unsuccessfully tried this:


rsync -avzP --exclude=$EXCLUDE root@$IP:$ORIGIN $DESTINATION/

What is wrong with the EXCLUDE syntax?
Old 05-15-2015, 02:38 PM   #2
LQ Newbie
Registered: Apr 2015
Posts: 3

Rep: Reputation: Disabled

Your problem is that bash expands braces before variables. So your effective command line (the one rsync is actually getting) includes the literal string --exclude={cristiano,marcelo}, as opposed to the brace-expanded version of that, namely --exclude=cristiano --exclude=marcelo, which would be what you want.

You might want to use an array. I use zsh, not bash, but on my system, after
EXCLUDE=(cris marc)
expands to --exclude=cris --exclude=marc.

Hope this helps,

Andrew Warshall
1 members found this post helpful.
Old 05-15-2015, 02:51 PM   #3
LQ Newbie
Registered: Dec 2014
Location: Finland
Posts: 3

Rep: Reputation: Disabled
I'm assuming the Bash shell here. I'm not sure about all the details of
this but when you run the first command you are using brace expansion:

bash-4.2$ echo cmd --exclude={foo,bar}
cmd --exclude=foo --exclude=bar
Now when we introduce the variable, we can see where things go wrong:

bash-4.2$ EXCLUDE={foo,bar}
bash-4.2$ echo cmd --exclude=$EXCLUDE
cmd --exclude={foo,bar}
One option would be to use an array instead:

bash-4.2$ EXCLUDE=(foo bar)
bash-4.2$ echo cmd ${EXCLUDE[@]/#/--exclude=}
cmd --exclude=foo --exclude=bar

Last edited by jhekkanen; 05-15-2015 at 02:53 PM.
1 members found this post helpful.
Old 05-15-2015, 08:15 PM   #4
Keith Hedger
Senior Member
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,493

Rep: Reputation: 634Reputation: 634Reputation: 634Reputation: 634Reputation: 634Reputation: 634
How about
EXCLUDE=$(echo --exclude={cristiano,marcelo})
echo rsync -avzP $EXCLUDE root@$IP:$ORIGIN $DESTINATION
rsync -avzP --exclude=cristiano --exclude=marcelo root@ samba
1 members found this post helpful.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
complex bash string substitution of csv file with multiline data Ryan_ Programming 16 03-31-2012 11:48 AM
[SOLVED] Bash string substitution porphyry5 Programming 1 05-14-2011 02:08 AM
string substitution lipun4u Programming 11 01-13-2011 02:47 AM
[SOLVED] Bash: functional difference between process substitution and here string with $( ) ? catkin Programming 2 03-15-2010 03:08 AM
String Substitution Swapna173 Linux - Newbie 1 08-04-2008 07:22 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:50 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration