Answers:
因为那是这些操作数的定义。从POSIX测试文档中,“操作数”部分:
s1 = s2
如果字符串s1和s2相同,则为true;否则为true。否则为假。
...
n1 -eq n2
如果整数n1和n2代数相等,则为true;否则为true。否则为假。
==
不是由POSIX定义的,它是的扩展bash
,从继承而来ksh
。==
当您想要便携性时,您不应该使用。从bash文档-Bash条件表达式:
字符串1 ==字符串2
字符串1 =字符串2
如果字符串相等,则为真。为测试POSIX一致性,应将'='与test命令一起使用。
以下序列可以更详尽地帮助您:
gnu:~$ [ sam -eq sam ]
bash: [: sam: integer expression expected
gnu:~$ echo "Exit status of \"[ sam -eq sam ]\" is $?."
Exit status of "[ sam -eq sam ]" is 2.
gnu:~$ [ 5 -eq 5 ]
gnu:~$ echo "Exit status of \"[ 5 -eq 5 ]\" is $?."
Exit status of "[ 5 -eq 5 ]" is 0.
gnu:~$ [ 5 = 5 ]
gnu:~$ echo "Exit status of \"[ 5 = 5 ]\" is $?."
Exit status of "[ 5 = 5 ]" is 0.
gnu:~$ [ sam = sam ]
gnu:~$ echo "Exit status of \"[ sam = sam ]\" is $?."
Exit status of "[ sam = sam ]" is 0.
gnu:~$ [ 5 == 5 ]
gnu:~$ echo "Exit status of \"[ 5 == 5 ]\" is $?."
Exit status of "[ 5 == 5 ]" is 0.
gnu:~$ [ sam == sam ]
gnu:~$ echo "Exit status of \"[ sam == sam ]\" is $?."
Exit status of "[ sam == sam ]" is 0.
gnu:~$ (( 5 == 5 ))
gnu:~$ echo "Exit status of \"(( 5 == 5 ))\" is $?."
Exit status of "(( 5 == 5 ))" is 0.
gnu:~$ (( sam == sam ))
gnu:~$ echo "Exit status of \"(( sam == sam ))\" is $?."
Exit status of "(( sam == sam ))" is 0.
gnu:~$ ( sam = sam )
The program 'sam' is currently not installed. You can install it by typing:
sudo apt-get install simon
gnu:~$ echo "Exit status of \"( sam = sam )\" is $?."
Exit status of "( sam = sam )" is 127.
gnu:~$ ( 5 = 5 )
5: command not found
gnu:~$ echo "Exit status of \"( 5 = 5 )\" is $?."
Exit status of "( 5 = 5 )" is 127.
gnu:~$ ( sam == sam )
The program 'sam' is currently not installed. You can install it by typing:
sudo apt-get install simon
gnu:~$ echo "Exit status of \"( sam == sam )\" is $?."
Exit status of "( sam == sam )" is 127.
gnu:~$ ( 5 == 5 )
5: command not found
gnu:~$ echo "Exit status of \"( 5 == 5 )\" is $?."
Exit status of "( 5 == 5 )" is 127.