我如何在Bash中的“if”语句中比较两个字符串变量?

时间:2021-01-30 00:06:16

I'm trying to get an if statement to work in Bash (using Ubuntu):

我试图在Bash中获得一个if语句(使用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

[你好:命令没有找到

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.

但它不工作。

11 个解决方案

#1


520  

For string comparison, use:

对于字符串比较,使用:

if [ "$s1" == "$s2" ]

For the a contains b, use:

a含b,使用:

if [[ $s1 == *"$s2"* ]]

(and make sure to add spaces between the symbols):

(并确保在符号之间添加空格):

bad:

缺点:

if ["$s1" == "$s2"]

good:

好:

if [ "$s1" == "$s2" ]

#2


131  

You need spaces:

你需要空间:

if [ "$s1" == "$s2" ]

#3


106  

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.

你需要离开的空白区域。

#4


26  

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.

注意openning/关闭括号和变量之间的空白,以及包装“=”符号的空格。

Also, be careful of your script header. It's not the same thing whether you use

同时,要注意你的脚本标题。这和你用的不一样。

#!/bin/bash

or

#!/bin/sh

Here's the source.

这是源。

#5


10  

I would suggest:

我建议:

#!/bin/bash

s1="hi"
s2="hi"

if [ $s1 = $s2 ]
then
  echo match
fi

Without the double quotes and with only one equals.

没有双引号,只有一个等于。

#6


8  

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. To execute use bash filename.bash - then you have to use ==.

主要的区别在于您使用的脚本语言。如果您正在使用bash,那么请包括#!/bin/bash在脚本的开头,将脚本保存为filename.bash。要执行使用bash文件名。bash—然后您必须使用==。

If you are using sh then use #!/bin/sh and save your script as filename.sh. To execute use sh filename.sh - then you have to use single =. Avoid intermixing them.

如果您正在使用sh,那么请使用#!/bin/sh并将脚本保存为filename.sh。执行使用sh文件名。sh -然后你必须使用single =。避免混合它们。

#7


7  

$ if [ "$s1" == "$s2" ]; then echo match; fi
match
$ test "s1" = "s2" ;echo match
match
$

#8


6  

I don't have access to a linux box right now, but [ is actually a program (and a bash builtin), so I think you have to put a space between [ and the first parameter.

我现在没有访问linux的权限,但实际上是一个程序(和一个bash构建程序),所以我认为您必须在(和第一个参数)之间放置一个空格。

Also note that string equality operator seems to be a single =

还要注意,字符串相等运算符似乎是一个单独的=。

#9


3  

This is more clarification than answer ! Yes , the clue is in the error message:

这比回答更清楚!是的,线索在错误信息中:

[hi: command not found

[你好:命令没有找到

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 "[".

因此,在打开后需要一个空格。

#10


2  

#!/bin/bash

s1="hi"
s2="hi"

if [ "x$s1" == "x$s2" ]
then
  echo match
fi

Adding additional string inside makes it more safe.

在内部添加额外的字符串会使其更加安全。

You could also use other notation for single line commands:

你也可以用其他的符号来表示单行命令:

[ "x$s1" == "x$s2" ] && echo match

#11


2  

For a version with pure Bash and without test, but really ugly, try:

对于一个纯Bash和没有测试的版本,但是非常丑陋,尝试:

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). This error is directed to /dev/null.

说明:在()一个额外的子壳被打开。如果有匹配,它会以0退出,如果没有匹配,则试图以$s1退出,这将导致错误(丑陋)。这个错误指向/dev/null。

#1


520  

For string comparison, use:

对于字符串比较,使用:

if [ "$s1" == "$s2" ]

For the a contains b, use:

a含b,使用:

if [[ $s1 == *"$s2"* ]]

(and make sure to add spaces between the symbols):

(并确保在符号之间添加空格):

bad:

缺点:

if ["$s1" == "$s2"]

good:

好:

if [ "$s1" == "$s2" ]

#2


131  

You need spaces:

你需要空间:

if [ "$s1" == "$s2" ]

#3


106  

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.

你需要离开的空白区域。

#4


26  

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.

注意openning/关闭括号和变量之间的空白,以及包装“=”符号的空格。

Also, be careful of your script header. It's not the same thing whether you use

同时,要注意你的脚本标题。这和你用的不一样。

#!/bin/bash

or

#!/bin/sh

Here's the source.

这是源。

#5


10  

I would suggest:

我建议:

#!/bin/bash

s1="hi"
s2="hi"

if [ $s1 = $s2 ]
then
  echo match
fi

Without the double quotes and with only one equals.

没有双引号,只有一个等于。

#6


8  

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. To execute use bash filename.bash - then you have to use ==.

主要的区别在于您使用的脚本语言。如果您正在使用bash,那么请包括#!/bin/bash在脚本的开头,将脚本保存为filename.bash。要执行使用bash文件名。bash—然后您必须使用==。

If you are using sh then use #!/bin/sh and save your script as filename.sh. To execute use sh filename.sh - then you have to use single =. Avoid intermixing them.

如果您正在使用sh,那么请使用#!/bin/sh并将脚本保存为filename.sh。执行使用sh文件名。sh -然后你必须使用single =。避免混合它们。

#7


7  

$ if [ "$s1" == "$s2" ]; then echo match; fi
match
$ test "s1" = "s2" ;echo match
match
$

#8


6  

I don't have access to a linux box right now, but [ is actually a program (and a bash builtin), so I think you have to put a space between [ and the first parameter.

我现在没有访问linux的权限,但实际上是一个程序(和一个bash构建程序),所以我认为您必须在(和第一个参数)之间放置一个空格。

Also note that string equality operator seems to be a single =

还要注意,字符串相等运算符似乎是一个单独的=。

#9


3  

This is more clarification than answer ! Yes , the clue is in the error message:

这比回答更清楚!是的,线索在错误信息中:

[hi: command not found

[你好:命令没有找到

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 "[".

因此,在打开后需要一个空格。

#10


2  

#!/bin/bash

s1="hi"
s2="hi"

if [ "x$s1" == "x$s2" ]
then
  echo match
fi

Adding additional string inside makes it more safe.

在内部添加额外的字符串会使其更加安全。

You could also use other notation for single line commands:

你也可以用其他的符号来表示单行命令:

[ "x$s1" == "x$s2" ] && echo match

#11


2  

For a version with pure Bash and without test, but really ugly, try:

对于一个纯Bash和没有测试的版本,但是非常丑陋,尝试:

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). This error is directed to /dev/null.

说明:在()一个额外的子壳被打开。如果有匹配,它会以0退出,如果没有匹配,则试图以$s1退出,这将导致错误(丑陋)。这个错误指向/dev/null。