[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

bash expansion

Posted on 2006-03-27
8
Medium Priority
?
558 Views
Last Modified: 2008-01-09
I'm writing a script to do a bunch of rsync'ing.  I noticed there's a difference between:

rsync -avuzb --exclude '*.pdf' src dst

and

X="--exclude '*.pdf'"
rsync -avuzb $X src dst

For some reason, the second example doesn't work even though it should be identical to the first.

So the get some insight, I turn on the special -x debug mode at a Bash prompt:

$ set -x

$ X="--exclude '*.pdf'"
+ X='--exclude '\''*.pdf'\'''


$ echo $X
+ echo --exclude ''\''*.pdf'\'''
--exclude '*.pdf'

So it appears that Bash is escaping the single-quotes in the double-quoted string.  Echo seems capable of stripping this out, but rsync doesn't seem to, and gets tripped up.

I realize there are workarounds, eg:

X="--exclude \*.pdf"

But I'm wondering why the more readable example above doesn't work?  What's so weird about single quotes within double quotes.

Thanks,
0
Comment
Question by:Kim Ryan
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 16306217
what do you get from
perl -e 'print map"{$_}",@ARGV' -- -avuzb $X src dst
and
perl -e 'print map"{$_}",@ARGV' -- -avuzb --exclude '*.pdf' src dst
rsync should see the same thing
0
 
LVL 19

Author Comment

by:Kim Ryan
ID: 16306885
I need to run this as a bash shell script, not Perl.
0
 
LVL 85

Expert Comment

by:ozo
ID: 16306893
I know, I just wanted to use perl to verify what the bash script was actually passing to rsync
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 27

Expert Comment

by:Nopius
ID: 16307678
rsync -avuzb --exclude '*.pdf' src dst
and
X="--exclude '*.pdf'"
rsync -avuzb $X src dst

These lines are not the same because  "--exclude '*.pdf'" becomes single non-splittable string and it is passed as 1 parameter to rsync
Using this syntax should avoid such behaveour:
X='*.pdf'
rsync -avuzb --exclude $X src dst



0
 
LVL 27

Expert Comment

by:Nopius
ID: 16307716
Sorry, my suggestion where incorrect, I've straced this command and and rsync receives argc=6.
So the problem is in escaping.
0
 
LVL 19

Author Comment

by:Kim Ryan
ID: 16307803
$ X="--exclude '*.pdf'"
$  perl -e 'print map"{$_}",@ARGV' -- -avuzb $X src dst
{-avuzb}{--exclude}{'*.pdf'}{src}{dst}
$ perl -e 'print map"{$_}",@ARGV' -- -avuzb --exclude '*.pdf' src dst
{-avuzb}{--exclude}{*.pdf}{src}{dst}

Hmmm... so when '*.pdf' is part of a double-quoted string, the single quotes remain intact as they get passed in as $X, whereas they get stripped out by the shell in the second example.

>Using this syntax should avoid such behaveour:
>X='*.pdf'
>rsync -avuzb --exclude $X src dst

This won't work (I tried) because the second line gets expanded out to:

rsync -avuzb --exclude *.pdf src dst

And the shell expands *.pdf into individual files...
0
 
LVL 27

Expert Comment

by:Nopius
ID: 16307823
You don't need extra double quotes for X:

X='--exclude *.pdf'
rsync -avuzb --exclude $X src dst

this works fine.
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 2000 total points
ID: 16314649
> rsync -avuzb --exclude '*.pdf' src dst
passes the literal string *.pdf to rsync

> $ X="--exclude '*.pdf'"
here the shell (bash) evaluates *.pdf and globs the matching files
also keep in mind that $X then is one string which contains spaces, probably something rsync does not understand.

> X="--exclude \*.pdf"
escapes the * from the shell, no file globing done when setting X
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Screencast - Getting to Know the Pipeline
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question