Skip to main content
8 events
when toggle format what by license comment
Aug 13, 2019 at 8:56 comment added cas There are lots of good questions and answers on this site about quoting variables (search for "quote variable") but this explains it extremely well: unix.stackexchange.com/questions/131766/…
Aug 13, 2019 at 8:49 comment added cas more on "most of the time, this is what you want" - unquoted variables tend to result in unfortunate (possibly disastrous) consequences if variables contain unexpected spaces or other shell meta-characters. a (contrived) example: say DIR='foo bar', and you have a directory containing sub-directories foo, bar, and foo bar. rm -rf $DIR will delete directories foo and bar. rm -rf "$DIR" will delete directory 'foo bar'. spaces are important. so are other meta-chars like & or ;. proper quoting is extremely important.
Aug 13, 2019 at 8:43 comment added cas if you have a variable, say $ARGS, that contains word1 word2 word3 then wrapping it in double-quotes when you use it (e.g. newvar="$VAR") will prevent the shell from expanding it into 3 separate arguments. 99.999999% of the time, this is what you want. If you don't wrap it in double-quotes, then the shell will expand it into three arguments - this is useful in cases where you want to use that variable to hold one or more arguments to another command (e.g. rsync $ARGS ...). And in most of those cases, you're better off using an array (e.g. rsync "${ARGS[@]}" ...)
Aug 12, 2019 at 4:23 comment added openCivilisation Thanks cas, can you explain what you mean by 'except where you actually want the shell's white space argument expansion to occur' ?
Aug 7, 2019 at 3:31 comment added cas +1. you should use double quotes around your variables except where you actually want the shell's white space argument expansion to occur. and, since you're using bash, you should probably use an array for the rsync args rather than $ARGS1, $ARGS2.
Aug 7, 2019 at 3:27 history edited cas CC BY-SA 4.0
added linefeeds to improve readability of comments, avoiding the dreaded horizontal scrollbar.
Aug 5, 2019 at 2:27 vote accept openCivilisation
Aug 5, 2019 at 2:27 history answered openCivilisation CC BY-SA 4.0