Quote:
Originally Posted by sluge
Hello,
I found a code where all variables a set by the following way:
VAR=""`ls -1`
Why "" put before the command?
|
the "" tells bash that what is inside is NOT TO BE EXPANDED
ALWAYS use quotes for strings and assignments unless your seeking planned flawless command substitution
see man bash(1) about expansion rules
consider:
VAR=`ls -1`
ls -1 may product "filenames with spaces" then you'd have
VAR=foo foo2
that's illegal, bash complains (if set -e is used, bash quits, see man bash(1))
now it's also possible a filename contains bad things like ! (exclamation mark, which bash thinks is special)
VAR=!foo
a disaster may happen
filename may contain $ and if name is $x
VAR=$x
VAR becomes the value of variable x - defintely not a good idea
VAR="$x" becomes variable x, yes, but bash does not apply commandline expantion to it, which can be a disaster. "$x" tells bash only certain expansions are allowed.
$ VAR=$x
$ VAR="$x"
AGAIN, may be the same: but may be disasterous without ""
VAR=`echo foo | tr -c "o" "i"` is a shortcut and often VAR=`ls` is shown in EXAMPLE scripts which omit "better practices" so to look thinner and be more clear. it's a shortcut but dont do it.
do you know if there are macros define in the envrionment that may be triggered? did you run the shell script in it's own invironment? did it inheirit what macro or global environment ? there are many more questions to ask about unquoted "full bash commandline substitution process" before running around without quotes
------------------------
there are too many more thigs that can go bad or really bad
in general:
ALWAYS use quotes for strings and assignments unless your seeking command substitution
glob * expands to '*' not '' if nothing is found, so be careful
even turning off globs is a safe practice