本文翻译自:How do I compare two string variables in an 'if' statement in Bash? [duplicate]
This question already has an answer here: 这个问题已经在这里有了答案:
- How to compare strings in Bash 10 answers 如何在Bash中比较字符串 10个答案
I'm trying to get an if
statement to work in Bash (using Ubuntu ): 我正在尝试让if
语句在Bash中工作(使用Ubuntu ):
#!/bin/bash
s1="hi"
s2="hi"
if ["$s1" == "$s2"]
then
echo match
fi
I've tried various forms of the if
statement, using [["$s1" == "$s2"]]
, with and without quotes, using =
, ==
and -eq
, but I still get the following error: 我已经尝试过各种形式的if
语句,使用[["$s1" == "$s2"]]
,使用和不使用引号,使用=
, ==
和-eq
,但是仍然出现以下错误:
[hi: command not found [hi:找不到命令
I've looked at various sites and tutorials and copied those, but it doesn't work - what am I doing wrong? 我查看了各种站点和教程并复制了这些站点和教程,但是它不起作用-我在做什么错?
Eventually, I want to say if $s1
contains $s2
, so how can I do that? 最终,我想说一下$s1
包含$s2
,那我该怎么做呢?
I did just work out the spaces bit.. :/ How do I say contains? 我只是算出空格位..:/我怎么说包含?
I tried 我试过了
if [[ "$s1" == "*$s2*" ]]
but it didn't work. 但这没用。
#1楼
参考:https://stackoom.com/question/Hwob/如何在Bash的-if-语句中比较两个字符串变量-重复
#2楼
You should be careful to leave a space between the sign of '[' and double quotes where the variable contains this: 您应该小心在变量中包含以下内容的'['和双引号之间留一个空格:
if [ "$s1" == "$s2" ]; then
# ^ ^ ^ ^
echo match
fi
The ^
s show the blank spaces you need to leave. ^
表示您需要离开的空格。
#3楼
For a version with pure Bash and without test
, but really ugly, try: 对于具有纯Bash且未经test
但非常丑陋的版本,请尝试:
if ( exit "${s1/*$s2*/0}" )2>/dev/null
then
echo match
fi
Explanation: In ( )
an extra subshell is opened. 说明:在( )
中打开了一个额外的子外壳。 It exits with 0 if there was a match, and it tries to exit with $s1 if there was no match which raises an error (ugly). 如果存在匹配项,则以0退出;如果不存在匹配项,则尝试以$ s1退出,从而引发错误(难看)。 This error is directed to /dev/null
. 该错误定向到/dev/null
。
#4楼
I suggest this one: 我建议这个:
if [ "$a" = "$b" ]
Notice the white space between the openning/closing brackets and the variables and also the white spaces wrapping the '=' sign. 请注意,左方括号和右方括号与变量之间的空白以及包裹“ =”符号的空白。
Also, be careful of your script header. 另外,请注意脚本头。 It's not the same thing whether you use 不管你用不一样
#!/bin/bash
or 要么
#!/bin/sh
#5楼
This is more clarification than answer ! 这比答案更清楚! Yes , the clue is in the error message: 是的,提示在错误消息中:
[hi: command not found [hi:找不到命令
which shows you that your "hi" has been concatenated to the "[". 这表明您的“ hi”已连接到“ [”。
Unlike in more traditional programming languages, in Bash, "[" is a command just like the more obvious "ls" etc. - it's not treated specially just because it's a symbol, hence the "[" and the (substituted) "$s1" which are immediately next to each other in your question, are joined (as is correct for Bash) and it then tries to find a command in that position: [hi - which is unknown to Bash. 与更传统的编程语言不同,在Bash中,“ [”就像更明显的“ ls”等一样是命令。-并不是仅仅因为它是一个符号就对其进行了特殊处理,因此“ [”和(替换)“ $ s1”在您的问题中紧挨着的“”(对于Bash来说是正确的)被加入,然后它尝试在该位置找到命令:[hi-Bash未知。
In C and some other languages, the "[" would be seen as a different "character class" and would be disjoint from the following "hi". 在C语言和某些其他语言中,“ [”将被视为不同的“字符类”,并且与后面的“ hi”是不相交的。
Hence you require a space after the opening "[". 因此,您需要在打开的“ [”后面留一个空格。
#6楼
This question has already great answers but here it appears that there is a slight confusion between using single equal and double equals in 这个问题已经有了很好的答案,但是在这里似乎在使用单等号和双等号之间存在一些混淆
if [ "$s1" == "$s2" ]
The main difference lies in which scripting language are you using. 主要区别在于您使用哪种脚本语言。 If you are using bash then include #!/bin/bash
in the starting of the script and save your script as filename.bash
. 如果您正在使用bash,则在脚本的开头包含#!/bin/bash
并将脚本另存为filename.bash
。 To execute use bash filename.bash
- then you have to use ==
. 要执行,请使用bash filename.bash
然后必须使用==
。
If you are using sh then use #!/bin/sh
and save your script as filename.sh
. 如果使用sh,则使用#!/bin/sh
并将脚本保存为filename.sh
。 To execute use sh filename.sh
- then you have to use single =
. 若要执行,请使用sh filename.sh
然后必须使用single =
。 Avoid intermixing them. 避免将它们混合在一起。