本文介绍了Linux shell 编程字符串比较语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
=
和==
在Linux shell编程中比较字符串有什么区别?
What is the difference between =
and ==
to compare strings in Linux shell programming?
也许下面的代码可以工作:
Maybe the following code works:
if [ "$NAME" = "user" ]
then
echo "your name is user"
fi
但我认为这不是正确的语法.它将用于通过 ==
语句比较字符串.
But I think it's not a correct syntax. It would be used to compare string by ==
statement.
什么是正确的?
推荐答案
这些页面解释了 bash 中的各种比较运算符:
These pages explain the various comparison operators in bash:
- http://www.tech-recipes.com/rx/209/bournebash-shell-scripts-string-comparison/
- http://tldp.org/LDP/abs/html/comparison-ops.html
- http://www.faqs.org/docs/Linux-HOWTO/Bash-Prog-Intro-HOWTO.html#ss11.2
在第二个链接页面上,您会发现:
On the second linked page, you will find:
==
is equal to
if [ "$a" == "$b" ]
This is a synonym for =.
这篇关于Linux shell 编程字符串比较语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!