shell脚本中字符串的不区分大小写的比较

时间:2022-07-05 20:12:32

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

==运算符用于比较shell脚本中的两个字符串。但是,我想比较两个忽略大小写的字符串,怎么做呢?这有什么标准命令吗?

11 个解决方案

#1


46  

if you have bash

如果你有bash

str1="MATCH"
str2="match"
shopt -s nocasematch
case "$str1" in
 $str2 ) echo "match";;
 *) echo "no match";;
esac

otherwise, you should tell us what shell you are using.

否则,你应该告诉我们你正在使用什么外壳。

alternative, using awk

替代方案,使用awk

str1="MATCH"
str2="match"
awk -vs1="$str1" -vs2="$str2" 'BEGIN {
  if ( tolower(s1) == tolower(s2) ){
    print "match"
  }
}'

#2


108  

In Bash, you can use parameter expansion to modify a string to all lower-/upper-case:

在Bash中,您可以使用参数扩展将字符串修改为所有低/大写:

var1=TesT
var2=tEst

echo ${var1,,} ${var2,,}
echo ${var1^^} ${var2^^}

#3


69  

All of these answers ignore the easiest and quickest way to do this (as long as you have Bash 4):

所有这些答案都忽略了最简单快捷的方法(只要你有Bash 4):

if [ "${var1,,}" = "${var2,,}" ]; then
  echo ":)"
fi

All you're doing there is converting both strings to lowercase and comparing the results.

你所做的就是将两个字符串转换为小写并比较结果。

#4


23  

Same as answer from ghostdog74 but slightly different code

与ghostdog74的回答相同但代码略有不同

shopt -s nocasematch
[[ "foo" == "Foo" ]] && echo "match" || echo "notmatch"
shopt -u nocasematch

#5


11  

One way would be to convert both strings to upper or lower:

一种方法是将两个字符串转换为上限或下限:

test $(echo "string" | /bin/tr '[:upper:]' '[:lower:]') = $(echo "String" | /bin/tr '[:upper:]' '[:lower:]') && echo same || echo different

Another way would be to use grep:

另一种方法是使用grep:

echo "string" | grep -qi '^String$' && echo same || echo different

#6


6  

For korn shell, I use typeset built-in command (-l for lower-case and -u for upper-case).

对于korn shell,我使用了排版内置命令(-l表示小写,-u表示大写)。

var=True
typeset -l var
if [[ $var == "true" ]]; then
    print "match"
fi

#7


3  

Very easy if you fgrep to do a case-insensitive line compare:

如果你fgrep做一个不区分大小写的行比较很容易:

str1="MATCH"
str2="match"

if [[ $(fgrep -ix $str1 <<< $str2) ]]; then
    echo "case-insensitive match";
fi

#8


2  

var1=match 
var2=MATCH 
if echo $var1 | grep -i "^${var2}$" > /dev/null ; then
  echo "MATCH"
fi

#9


2  

Here is my solution using tr:

这是我使用tr的解决方案:

var1=match
var2=MATCH
var1=`echo $var1 | tr '[A-Z]' '[a-z]'`
var2=`echo $var2 | tr '[A-Z]' '[a-z]'`
if [ "$var1" = "$var2" ] ; then
  echo "MATCH"
fi

#10


1  

shopt -s nocaseglob

shopt -s nocaseglob

#11


0  

For zsh the syntax is slightly different:

对于zsh,语法略有不同:

> str1='MATCH'
> str2='match'
> [ "$str1" == "$str2:u" ] && echo 'Match!'
Match!
>

This will convert str2 to uppercase before the comparison.

这将在比较之前将str2转换为大写。

More examples for changing case below:

更改以下案例的更多示例:

> xx=Test
> echo $xx:u
TEST
> echo $xx:l
test

#1


46  

if you have bash

如果你有bash

str1="MATCH"
str2="match"
shopt -s nocasematch
case "$str1" in
 $str2 ) echo "match";;
 *) echo "no match";;
esac

otherwise, you should tell us what shell you are using.

否则,你应该告诉我们你正在使用什么外壳。

alternative, using awk

替代方案,使用awk

str1="MATCH"
str2="match"
awk -vs1="$str1" -vs2="$str2" 'BEGIN {
  if ( tolower(s1) == tolower(s2) ){
    print "match"
  }
}'

#2


108  

In Bash, you can use parameter expansion to modify a string to all lower-/upper-case:

在Bash中,您可以使用参数扩展将字符串修改为所有低/大写:

var1=TesT
var2=tEst

echo ${var1,,} ${var2,,}
echo ${var1^^} ${var2^^}

#3


69  

All of these answers ignore the easiest and quickest way to do this (as long as you have Bash 4):

所有这些答案都忽略了最简单快捷的方法(只要你有Bash 4):

if [ "${var1,,}" = "${var2,,}" ]; then
  echo ":)"
fi

All you're doing there is converting both strings to lowercase and comparing the results.

你所做的就是将两个字符串转换为小写并比较结果。

#4


23  

Same as answer from ghostdog74 but slightly different code

与ghostdog74的回答相同但代码略有不同

shopt -s nocasematch
[[ "foo" == "Foo" ]] && echo "match" || echo "notmatch"
shopt -u nocasematch

#5


11  

One way would be to convert both strings to upper or lower:

一种方法是将两个字符串转换为上限或下限:

test $(echo "string" | /bin/tr '[:upper:]' '[:lower:]') = $(echo "String" | /bin/tr '[:upper:]' '[:lower:]') && echo same || echo different

Another way would be to use grep:

另一种方法是使用grep:

echo "string" | grep -qi '^String$' && echo same || echo different

#6


6  

For korn shell, I use typeset built-in command (-l for lower-case and -u for upper-case).

对于korn shell,我使用了排版内置命令(-l表示小写,-u表示大写)。

var=True
typeset -l var
if [[ $var == "true" ]]; then
    print "match"
fi

#7


3  

Very easy if you fgrep to do a case-insensitive line compare:

如果你fgrep做一个不区分大小写的行比较很容易:

str1="MATCH"
str2="match"

if [[ $(fgrep -ix $str1 <<< $str2) ]]; then
    echo "case-insensitive match";
fi

#8


2  

var1=match 
var2=MATCH 
if echo $var1 | grep -i "^${var2}$" > /dev/null ; then
  echo "MATCH"
fi

#9


2  

Here is my solution using tr:

这是我使用tr的解决方案:

var1=match
var2=MATCH
var1=`echo $var1 | tr '[A-Z]' '[a-z]'`
var2=`echo $var2 | tr '[A-Z]' '[a-z]'`
if [ "$var1" = "$var2" ] ; then
  echo "MATCH"
fi

#10


1  

shopt -s nocaseglob

shopt -s nocaseglob

#11


0  

For zsh the syntax is slightly different:

对于zsh,语法略有不同:

> str1='MATCH'
> str2='match'
> [ "$str1" == "$str2:u" ] && echo 'Match!'
Match!
>

This will convert str2 to uppercase before the comparison.

这将在比较之前将str2转换为大写。

More examples for changing case below:

更改以下案例的更多示例:

> xx=Test
> echo $xx:u
TEST
> echo $xx:l
test