Bash compare operator always true

拟墨画扇 提交于 2019-11-28 14:12:41

Bash commands are sensitive to spaces. You need to add spaces around ==.

Observe that this gives the wrong answer:

$ IP_EX=abc; [[ "$IP_EX"=="173.199.65" ]] && echo True
True

By contrast, this version, with spaces, works correctly:

$ IP_EX=abc; [[ "$IP_EX" == "173.199.65" ]] && echo True
$ 

The problem is that bash sees "$IP_EX"=="173.199.65" as a single string. When given such a single argument, [[ returns true if the string is not empty and false if it is empty:

$ [[ "" ]] && echo True
$ [[ "1" ]] && echo True
True

With the spaces added in, bash sees "$IP_EX" == "173.199.65" as three arguments with the middle argument being ==. It therefore tests for equality. This is what you want.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!