1

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

web_dir=/tng4/users/ldiao/AQF/wrf-chem-result/
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!

1
  • 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. Commented Aug 21, 2013 at 16:51

1 Answer 1

6

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.

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

3 Comments

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 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.
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.

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.