tu4n
tu4n

Reputation: 4460

Bash shell scripting. Variable as argument. No quote vs double quote

I have 2 different commands:

rm -rf $MY_DIR
rm -rf "$MY_DIR"

What is the difference between them? Which should I use?

Upvotes: 0

Views: 74

Answers (1)

Christopher Shroba
Christopher Shroba

Reputation: 7584

Shell variables are expanded verbatim, so you should use the quoted option. Let's say your $HOME variable were equal to /Users/rocket spacer. In that case, it would be the difference between running rm -rf /Users/rocket spacer and rm -rf "/Users/rocket spacer". The first would try to remove two things: /Users/rocket and spacer in the current directory. The second option would do what you want, and remove the the directory /Users/rocket spacer. In general, when expanding a shell variable which represents a path, you should wrap it in quotes so you don't have to worry about the possibility of anything being misinterpreted.

Upvotes: 2

Related Questions