简体   繁体   中英

(multiple) double quote and variable

putting multiple double quote to a bash variable seems have the same result to putting one or no double quote.

eg, t1=a , t2="a" , t3=""a"" , echo them prints the same result: a . Are there any difference?

Finally, what the difference between if [ $x == "valid" ]; then if [ $x == "valid" ]; then , if [ ""$x"" == "valid" ]; then if [ ""$x"" == "valid" ]; then and if [ "$x" == "valid" ]; then if [ "$x" == "valid" ]; then ?

Consecutive quotes are simply superfluous .

This doesn't imply that an unquoted variable behaves similarly to a quoted one.

The following example should clarify it:

$ set -x                       # enable xtrace
$ foo=""
+ foo=
$ [ -n $foo ] && echo y        # unquoted variable results in incorrect result for "string not empty" check
+ '[' -n ']'
+ echo y
y
$ [ -n "$foo" ] && echo y      # quoted variable results in correct result for the same check
+ '[' -n '' ']'
$ [ -n ""$foo"" ] && echo y
+ '[' -n '' ']'
$ echo "hey"
+ echo hey
hey
$ echo ""hey""                 # produces same result as in the previous case
+ echo hey
hey
$ echo """hey"""
+ echo hey
hey

A quote in bash is like a switch. When bash sees a quote, it switches quoting on. When it sees another quote, it switches it OFF.

Hence echo ""a"" is same as echo a

If x is empty or unset, then you will have to provide quotes around the variable, otherwise bash will give an error.

Quoting is very important when you are dealing with files. $ is just a substitution command and if your filename has a space, then $ simply expands the filename. If you do not have quotes around it then bash will treat it like two words instead of one.

Check this tutorial. http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_03.html

Double double quotes is an empty double quote. It is not strictly meaningless because it can count as an argument, for instance: echo ab is different from echo a "" b . The empty double quotes is treated as an argument and so two spaces are printed out between a an b.

echo ""a and t3=""a"" are pointless uses of empty double quotes. The empty string is included as part of the a, so it has no effect.

The proper way to do a variable check is this: if [ "$x" = "some text" ]; then ... if [ "$x" = "some text" ]; then ... If x is empty, it will be treated as an empty argument (which is the desired behavior.)

If you skip the quotes: if [ $x = "this is bad" ]; then ... if [ $x = "this is bad" ]; then ... you will have a problem if x is empty, or if it contains spaces. The test would not even see the x at all, and = would be the first argument.

Using double double quotes around the x: if [ ""$x"" = "this is bad" ]; then ... if [ ""$x"" = "this is bad" ]; then ... would actually still work properly if x is empty. It would have a problem, however, if x contained spaces. eg x="ab" . The test would see a and b as separate arguments, which does not make sense.

If you want to include a literal double quote character in an argument, use a backslash: echo "\\"" or just echo \\" .

Ultimately the whole point of double quotes is manipulating how the command line is split into arguments. echo "a" , echo a and echo ""a"" all have a single 'a' character as their first argument, and are therefore exactly equivalent. echo ab and echo "ab" are not equivalent. The first has two arguments and the second only has one. In the first case, the double space will be collapsed to a single space separating the arguments. The double space will be preserved as part of the argument in the second case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM