Take the 2-minute tour ×

For me bash script is always some kind of tricky. I have

rm "$web_dir""three_days_ago/*.gif"

the error message is

rm: cannot remove '/tng4/users/ldiao/AQF/wrf-chem-result/three_days_ago/*.gif': 
No such file or directory

But if I change to

rm "$web_dir"three_days_ago/*.gif""

then it works. There is no spell errors. Can someone explains for me the reason? thanks!

share|improve this question
The globbing characters *, ? etc are not expanded within quotes, but they are interpreted literally. To find out why they designed it like this, you would probably have to ask one of the language's developers. –  user000001 Aug 21 '13 at 16:51
add comment

1 Answer

up vote 6 down vote accepted

The reason is that bash globbing does not work inside " "

Correct variant would be

rm -- "$web_dir/three_days_ago/"*.gif

Also, use -i option when playing with rm. This way if you make a mistake or a typo it wont delete all of your files unless you confirm that.

share|improve this answer
And just to be precise, your second example works because you have $web_dir properly in double quotes, followed by unquoted three_days_ago/*.gif, followed by a completely superfluous double-quoted empty string. For what it's worth, the idiomatic way to quote that would be something like "${web_dir}three_days_ago"/*.gif –  tripleee Aug 21 '13 at 17:55
@tripleee I always suggest to use extra / between variables. Just in case you forget to end your variable with a slash. Two slashes wont hurt. So I'd edit your example to "${web_dir}/three_days_ago"/*.gif. Also it seems like there's no difference if you put a slash after the quote or right before it. –  Aleks-Daniel Jakimenko Aug 21 '13 at 18:05
Right, the slash is not a metacharacter to the shell, so it doesn't require quoting; but on the other hand, quoting regular characters is harmless. –  tripleee Aug 22 '13 at 4:44
add comment

Your Answer


