Always be careful with shell expansion and variable names. It is generally a good idea to enclose variable calls in double quotation marks, unless you have a good reason not to. Similarly, if you are directly following a variable name with alphanumeric text, be sure also to enclose the variable name in curly braces ({}) to distinguish it from the surrounding text. Otherwise, the shell interprets the trailing text as part of your variable name -- and most likely returns a null value.
Listing 1 provides examples of various quotation and non-quotation of variables and their effects.
Listing 1. Example of good habit #4: Quoting (and not quoting) a variable
~ $ ls tmp /a b
~ $ VAR="tmp/*"
~ $ echo $VAR tmp/a tmp/b
~ $ echo "$VAR" tmp/*
~ $ echo $VARa
~ $ echo "$VARa"
~ $ echo "${VAR}a"
tmp/*a
~ $ echo ${VAR}a
tmp/a
~ $ |
No comments:
Post a Comment