我最近学习了如何在Shell中编程。
我不明白为什么这两个语句产生不同的输出。似乎如果没有空格,测试会将10==11视为字符串,并始终返回true。

$test 10==11 && echo yes || echo no
$yes
$test 10 == 11 && echo yes || echo no
$no

最佳答案

# single string, not null so true, and result is yes
test 10==11 && echo yes || echo no

# there exist space 10 == 11 not equal string comparison so result no
test 10 == 11 && echo yes || echo no


Read More Here

等价物

if test 10==11; then echo yes; else echo no; fi
yes

if test 10 == 11; then echo yes; else echo no; fi
no

# or this same as above
if test 10 = 11; then echo yes; else echo no; fi
no


来自http://tldp.org/LDP/abs/html/comparison-ops.html

string comparison

=

    is equal to

    if [ "$a" = "$b" ]

    Caution

    Note the whitespace framing the =.

    if [ "$a"="$b" ] is not equivalent to the above.
==

    is equal to

    if [ "$a" == "$b" ]

关于linux - Linux Shell:条件语句中的空格,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45303312/

10-16 09:24