如何在 bash 中的 if 条件下比较两个字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20742474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I compare two strings in if condition in bash
提问by Sanshayan
s="STP=20"
if [[ "$x" == *"$s"* ]]
The ifcondition is always false; why?
该if条件始终是假的; 为什么?
采纳答案by Ferenc Deak
Try this: http://tldp.org/LDP/abs/html/comparison-ops.html
试试这个:http: //tldp.org/LDP/abs/html/comparison-ops.html
string comparison
=
is equal to
if [ "$a" = "$b" ]
回答by David W.
There is a difference in testing for equality between [ ... ]and [[ ... ]].
[ ... ]和之间的相等性测试存在差异[[ ... ]]。
The [ ... ]is an alias to the testcommand:
的[ ... ]是别名的test命令:
STRING1 = STRING2 the strings are equal
STRING1 = STRING2字符串相等
However, when using [[ ... ]]
然而,当使用 [[ ... ]]
When the
==and!=operators are used, the string to the right of the operator is considered a pattern and matched according to the rules described below under Pattern Matching. If the shell option nocasematch is enabled, the match is performed without regard to the case of alphabetic characters. The return value is 0 if the string matches (==) or does not match (!=) the pattern, and 1 otherwise. Any part of the pattern may be quoted to force it to be matched as a string.
当使用
==and!=运算符时,运算符右侧的字符串被视为一个模式,并根据下面模式匹配中描述的规则进行匹配。如果启用了 shell 选项 nocasematch,则执行匹配时不考虑字母字符的大小写。如果字符串匹配 (==) 或不匹配 (!=) 模式,则返回值为 0 ,否则为 1。可以引用模式的任何部分以强制将其作为字符串进行匹配。
The same seems to be true with just the =sign:
仅使用=符号似乎也是如此:
$ foo=bar
$ if [[ $foo = *ar ]]
> then
> echo "These patterns match"
> else
> echo "These two strings aren't equal"
> fi
These patterns match
Note the difference:
注意区别:
$ foo=bar
> if [ $foo = *ar ]
> then
> echo "These patterns match"
> else
> echo "These two strings aren't equal"
> fi
These two strings aren't equal
However, there are a few traps with the [ $f00 = *ar ]syntax. This is the same as:
但是,[ $f00 = *ar ]语法中有一些陷阱。这与:
test $foo = *ar
Which means the shell will interpolate glob expressions and variables before executing the statement. If $foois empty, the command will become equivalent to:
这意味着 shell 将在执行语句之前插入 glob 表达式和变量。如果$foo为空,则该命令将等效于:
test = *ar # or [ = *ar ]
Since the =isn't a valid comparison operator in test, you'll get an error like:
由于 中的=不是有效的比较运算符test,您将收到如下错误:
bash: [: =: unary operator expected
Which means the [was expecting a parameter found in the testmanpage.
这意味着[期望在test联机帮助页中找到一个参数。
And, if I happen to have a file barin my directory, the shell will replace *arwith all files that match that pattern (in this case bar), so the command will become:
而且,如果我的目录中碰巧有一个文件bar,shell 将替换*ar为与该模式匹配的所有文件(在本例中为bar),因此命令将变为:
[ $foo = bar ]
which IS true.
这是真的。
To get around the various issues with [ ... ], you should always put quotes around the parameters. This will prevent the shell from interpolating globs and will help with variables that have no values:
要解决 的各种问题[ ... ],您应该始终在参数周围加上引号。这将防止 shell 插入 globs 并有助于处理没有值的变量:
[ "$foo" = "*ar" ]
This will test whether the variable $foois equal to the string *ar. It will work even if $foois empty because the quotation marks will force an empty string comparison. The quotes around *arwill prevent the shell from interpolating the glob. This is a true equality.
这将测试变量$foo是否等于字符串*ar。即使$foo为空它也能工作,因为引号将强制进行空字符串比较。周围的引号*ar将阻止 shell 插入 glob。这是真正的平等。
Of course, it just so happens that if you use quotation marks when using [[ ... ]], you'll force a string match too:
当然,碰巧的是,如果您在 using 时使用引号[[ ... ]],您也会强制进行字符串匹配:
foo=bar
if [[ $foo == "*ar" ]]
then
echo "This is a pattern match"
else
echo "These strings don't match"
fi
So, in the end, if you want to test for string equality, you can use either [ ... ]or [[ ... ]], but you must quote your parameters. If you want to do glob pattern matching, you must leave off the quotes, and use [[ ... ]].
所以,最后,如果你想测试字符串是否相等,你可以使用[ ... ]或[[ ... ]],但你必须引用你的参数。如果要进行 glob 模式匹配,则必须去掉引号,并使用[[ ... ]].
回答by Jens
To compare two strings in variables xand yfor equality, use
要比较变量中的两个字符串x并y使其相等,请使用
if test "$x" = "$y"; then
printf '%s\n' "equal"
else
printf '%s\n' "not equal"
fi
To test whether xappears somewhere in y, use
要测试是否x出现在 中的某处y,请使用
case $y in
(*"$x"*)
printf '%s\n' "$y contains $x"
;;
(*)
printf '%s\n' "$y does not contain $x"
;;
esac
Note that these constructs are portable to any POSIX shell, not just bash. The [[ ]]construct for tests is not (yet) a standard shell feature.
请注意,这些构造可移植到任何 POSIX shell,而不仅仅是 bash。该[[ ]]用于测试结构是(还)没有一个标准壳特征。
回答by cforbish
I do not know where you came up with the *, but you were real close:
我不知道你从哪里想到的*,但你真的很接近:
s="STP=20"
if [[ "STP=20" == "$s" ]]; then
echo "It worked!"
fi
回答by Suvarna Pattayil
You need to escape =using \in the string s="STP=20"
您需要在字符串中=使用转义\s="STP=20"
s="STP\=20"
if [[ "STP\=20" == "$s" ]]; then echo Hi; else echo Bye; fi

