Skip to main content
added 68 characters in body
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

(( .. )) is an arithmetic construct, and in arithmetic contexts, a string is taken as the name of a variable, and the value of that variable is used. This happens after $var expansions are expanded, so your script looks at variables called nfosys and Infosys. With both variables unset, both are taken to be zero, i.e. equal. But:

$ str1=foo str2=bar foo=1
$ (( $str1 == $str2 )) && echo true || echo false
false   

See e.g. Bash's manual on shell arithmetic:

Shell variables are allowed as operands; parameter expansion is performed before the expression is evaluated. Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax. A shell variable that is null or unset evaluates to 0 when referenced by name without using the parameter expansion syntax.

For string comparison, use

[ "$STR_1" = "$STR_2" ]   # in aany POSIX shell, or

[[ $STR_1 === $STR_2"$STR_2" ]]    # in Bash/ksh/zsh

The former needs quotes due to word-splitting, the latter works with or without themneeds it only on the right-hand side (in bash / ksh) for $STR_2 not to be taken as a pattern.

Also see: What is the difference between the Bash operators [[ vs [ vs ( vs ((?

(( .. )) is an arithmetic construct, and in arithmetic contexts, a string is taken as the name of a variable, and the value of that variable is used. This happens after $var expansions are expanded, so your script looks at variables called nfosys and Infosys. With both variables unset, both are taken to be zero, i.e. equal. But:

$ str1=foo str2=bar foo=1
$ (( $str1 == $str2 )) && echo true || echo false
false   

See e.g. Bash's manual on shell arithmetic:

Shell variables are allowed as operands; parameter expansion is performed before the expression is evaluated. Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax. A shell variable that is null or unset evaluates to 0 when referenced by name without using the parameter expansion syntax.

For string comparison, use

[ "$STR_1" = "$STR_2" ]   # in a POSIX shell, or

[[ $STR_1 == $STR_2 ]]    # in Bash/ksh/zsh

The former needs quotes due to word-splitting, the latter works with or without them.

Also see: What is the difference between the Bash operators [[ vs [ vs ( vs ((?

(( .. )) is an arithmetic construct, and in arithmetic contexts, a string is taken as the name of a variable, and the value of that variable is used. This happens after $var expansions are expanded, so your script looks at variables called nfosys and Infosys. With both variables unset, both are taken to be zero, i.e. equal. But:

$ str1=foo str2=bar foo=1
$ (( $str1 == $str2 )) && echo true || echo false
false   

See e.g. Bash's manual on shell arithmetic:

Shell variables are allowed as operands; parameter expansion is performed before the expression is evaluated. Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax. A shell variable that is null or unset evaluates to 0 when referenced by name without using the parameter expansion syntax.

For string comparison, use

[ "$STR_1" = "$STR_2" ]   # in any POSIX shell, or

[[ $STR_1 = "$STR_2" ]]   # in Bash/ksh/zsh

The former needs quotes due to word-splitting, the latter needs it only on the right-hand side (in bash / ksh) for $STR_2 not to be taken as a pattern.

Also see: What is the difference between the Bash operators [[ vs [ vs ( vs ((?

added 110 characters in body
Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441

(( .. )) is an arithmetic construct, and in arithmetic contexts, a string is taken as the name of a variable, and the value of that variable is used. This happens after $var expansions are expanded, so your script looks at variables called nfosys and Infosys. With both variables unset, both are taken to be zero, i.e. equal. But:

$ str1=foo str2=bar foo=1
$ (( $str1 == $str2 )) && echo true || echo false
false   

See e.g. Bash's manual on shell arithmetic:

Shell variables are allowed as operands; parameter expansion is performed before the expression is evaluated. Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax. A shell variable that is null or unset evaluates to 0 when referenced by name without using the parameter expansion syntax.

Use [ "$STR_1" = "$STR_2" ] forFor string comparison in a POSIX shell, or [[ $STR_1 == $STR_2 ]] in Bash/ksh/zsh. (Theuse

[ "$STR_1" = "$STR_2" ]   # in a POSIX shell, or

[[ $STR_1 == $STR_2 ]]    # in Bash/ksh/zsh

The former needs quotesneeds quotes due to word-splitting, the latter can useworks with or without them.)

Also see: What is the difference between the Bash operators [[ vs [ vs ( vs ((?

(( .. )) is an arithmetic construct, and in arithmetic contexts, a string is taken as the name of a variable, and the value of that variable is used. This happens after $var expansions are expanded, so your script looks at variables called nfosys and Infosys. With both variables unset, both are taken to be zero, i.e. equal. But:

$ str1=foo str2=bar foo=1
$ (( $str1 == $str2 )) && echo true || echo false
false   

See e.g. Bash's manual on shell arithmetic:

Shell variables are allowed as operands; parameter expansion is performed before the expression is evaluated. Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax. A shell variable that is null or unset evaluates to 0 when referenced by name without using the parameter expansion syntax.

Use [ "$STR_1" = "$STR_2" ] for string comparison in a POSIX shell, or [[ $STR_1 == $STR_2 ]] in Bash/ksh/zsh. (The former needs quotes, the latter can use them.)

Also see: What is the difference between the Bash operators [[ vs [ vs ( vs ((?

(( .. )) is an arithmetic construct, and in arithmetic contexts, a string is taken as the name of a variable, and the value of that variable is used. This happens after $var expansions are expanded, so your script looks at variables called nfosys and Infosys. With both variables unset, both are taken to be zero, i.e. equal. But:

$ str1=foo str2=bar foo=1
$ (( $str1 == $str2 )) && echo true || echo false
false   

See e.g. Bash's manual on shell arithmetic:

Shell variables are allowed as operands; parameter expansion is performed before the expression is evaluated. Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax. A shell variable that is null or unset evaluates to 0 when referenced by name without using the parameter expansion syntax.

For string comparison, use

[ "$STR_1" = "$STR_2" ]   # in a POSIX shell, or

[[ $STR_1 == $STR_2 ]]    # in Bash/ksh/zsh

The former needs quotes due to word-splitting, the latter works with or without them.

Also see: What is the difference between the Bash operators [[ vs [ vs ( vs ((?

Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441

(( .. )) is an arithmetic construct, and in arithmetic contexts, a string is taken as the name of a variable, and the value of that variable is used. This happens after $var expansions are expanded, so your script looks at variables called nfosys and Infosys. With both variables unset, both are taken to be zero, i.e. equal. But:

$ str1=foo str2=bar foo=1
$ (( $str1 == $str2 )) && echo true || echo false
false   

See e.g. Bash's manual on shell arithmetic:

Shell variables are allowed as operands; parameter expansion is performed before the expression is evaluated. Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax. A shell variable that is null or unset evaluates to 0 when referenced by name without using the parameter expansion syntax.

Use [ "$STR_1" = "$STR_2" ] for string comparison in a POSIX shell, or [[ $STR_1 == $STR_2 ]] in Bash/ksh/zsh. (The former needs quotes, the latter can use them.)

Also see: What is the difference between the Bash operators [[ vs [ vs ( vs ((?