4 ms·
I've started quoting arguments in shell scripts even when it's not technically necessary to avoid problems with spaces. I can't count how many scripts I've writ
by tlrobinson 15y ago
I've started quoting arguments in shell scripts even when it's not technically necessary to avoid problems with spaces. I can't count how many scripts I've written/encountered that didn't work with a path containing a space (apparently much more common with OS X users than Linux users)
This wouldn't delete the correct directory, but at least it won't delete "/usr" either:
rm -rf "/usr /lib/nvidia-current/xorg/xorg"
There are lots of other pitfalls associated with not quoting things in shell scripts, like this common one:
if [ $foo -eq "bar" ];
will cause an error if $foo isn't set or is an empty string, while this will work correctly:
if [ "$foo" -eq "bar" ];
Bonus that your syntax highlighter can highlight the arguments. My rule is that flags aren't quoted, but paths and other parameters are.
- whacker 15y agoI always use set -eu. Makes my scripts much safer and less unpredictable.