Let's suppose that you have a variable which is subject to word splitting, globing and pattern matching:
var='*
.?'
While I'm pretty sure that everyone agrees that "$var" is the best way to expand the variable as a string literal, I've identified a few cases where you don't need to use the double quotes:
- Simple assignment: - x=$var
- Case statement: - case $var in ...
- Leftmost part of bash test construct: - [[ $var .... ]]
- UPDATE1: Bash here-string: - <<< $varwhich works starting from bash-4.4 (thank you @GordonDavidson)
- UPDATE2: Exported assignment (in bash): - export x=$var
Is it correct? Is there any other shell/bash statement where the variable isn't subject to glob expansion or word splitting without using double-quotes? where expanding a variable with or without double quotes is 100% equivalent?
The reason why I ask this question is that when reading foreign code, knowing the above mentioned border-cases might help.
For example, one bug that I found in a script that I was debugging is something like:
out_exists="-f a.out"
[[ $out_exists ]] && mv a.out prog.exe
mv: cannot stat ‘a.out’: No such file or directory
 
     
    