Why is [ “$foo”==“$bar” ] always true in bash? [duplicate]

别来无恙 提交于 2019-12-02 02:46:27

In bash, spaces count. Replace:

if [ "$Encode"=="$EncOrDec" ]; then

With:

if [ "$Encode" = "$EncOrDec" ]; then

Without spaces, bash is just testing whether the string "$Encode"=="$EncOrDec" is empty or not. Since it is never empty, the then clause is always executed.

Also, as a minor detail, when using [...], the use of = for string equality is POSIX standard. Bash accepts == but == is not standard and won't be reliably portable.

The same applies to the elif line. Replace:

elif [ "$Decode"=="$EncOrDec" ]; then

With:

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