Which of the following quoting styles, for GNU Bash variables, is preferred and why?
- Two double quotes:
VAR="/path/$V1/path with space/$V2". - Multiple double quotes:
VAR=/path/"$V1"/"path with space"/"$V2". - Combination:
VAR="/path/"$V1"/path with space/"$V2"". - Other.
Preferred here meaning, works as intended in as many cases as possible without being unnecessarily verbose, such that it can be used consistently without any issues. If there are (rare?) special cases, then please restrict it to filesystem paths.
Assume that the path part can contain spaces or special characters. For 2 this of course means that those path parts need to be quoted as well.
Basically this seems to boil down to another question: Does quoting the variable substitutions individually do something extra?
If this was true, then it would motivate 2 and 3 (to avoid having to quote each path part with space individually). If not, then 1 seems preferred since it is the simplest.
However, looking for example here, 2 is most common by far, though as a special case VAR="$OTHER_VAR"/path/to/something?
VAR="$HOME"/path/to/something)? – Klorax Feb 18 '19 at 20:52/path/to/dir/file"nameYou will needfile\"namethere – Sergiy Kolodyazhnyy Feb 18 '19 at 21:32