Bash detailed line abbreviations

I am comparing two lines in a bash script as follows:

x="hello" y="hello" if [[ "$x" != "$y" ]]; then echo "different" else echo "same" fi 

This comparison works. When I execute the script with -x , the comparison still works, but it shows the output

 + x=hello + y=hello + [[ -n hello ]] + [[ hello != \h\e\l\l\o ]] + echo same 

I'm curious why the right side of the line is displayed as \h\e\l\l\o and not hello

+5
source share
2 answers

A simple explanation is for the same reason that the left side does not have quotation marks around it.

-x shows you an equivalent but not exact idea of ​​what it was running. The right part = and != In [[ ... ]] matches the pattern.

From the manual:

When the == and! Operators are used =, the line to the right of the statement is considered a template and matched according to the rules described below in the Match Matching section ..... Any part of the template can be quoted to make it fit the line.

The -x output, for some reason, prefers to use escaping instead of quoting to disable the corresponding pattern.

+4
source

When using = , == and != In [[ right line may contain globes ( * , ? , Etc.).

Backslashes are not needed in your example, although they do not hurt. They are necessary if the line on the right contains a possible wildcard character. For instance:

 $ set -x $ [[ hi == 'hi*' ]]; echo $? + [[ hi == \h\i\* ]] + echo 1 1 $ [[ hi == hi* ]]; echo $? + [[ hi == hi* ]] + echo 0 0 
0
source

Source: https://habr.com/ru/post/1206107/


All Articles