bash bash中不区分大小写的字符串比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13848101/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:00:57  来源:igfitidea点击:

case insensitive string comparison in bash

bashstring-comparison

提问by jjacobs

The following line removes the leading text before the variable $PRECEDING

以下行删除变量前的前导文本 $PRECEDING

temp2=${content#$PRECEDING}

But now i want the $PRECEDINGto be case-insensitive. This works with sed's Iflag. But i can't figure out the whole cmd.

但现在我希望$PRECEDING不区分大小写。这适用于sed'sI标志。但我无法弄清楚整个 cmd。

采纳答案by sampson-chen

Here's a way to do it with sed:

这是使用sed执行此操作的一种方法:

temp2=$(sed -e "s/^.*$PRECEDING//I" <<< "$content")

Explanation:

解释:

  • ^.*$PRECEDING: ^means start of string, .means any character, .*means any character zero or more times. So together this means "match any pattern from start of string that is followed by (and including) string stored in $PRECEDING.
  • The Ipart means case-insensitive, the gpart (if you use it) means "match all occurrences" instead of just the 1st.
  • The <<<notation is for herestrings, so you save an echo.
  • ^.*$PRECEDING:^表示字符串的开始,.表示任意字符,.*表示任意字符零次或多次。因此,这意味着“匹配从字符串开头开始的任何模式,后跟(并包括)存储在$PRECEDING.
  • I部分表示不区分大小写,该g部分(如果您使用它)表示“匹配所有出现”而不仅仅是第一个。
  • <<<符号用于herestrings,因此您保存了一个echo.

回答by Riot

No need to call out to sed or use shopt. The easiest and quickest way to do this (as long as you have Bash 4):

无需调用 sed 或使用 shopt。执行此操作的最简单快捷的方法(只要您有 Bash 4):

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

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

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

回答by glenn Hymanman

The only bash way I can think of is to check if there's a match (case-insensitively) and if yes, exclude the appropriate number of charactersfrom the beginning of $content:

我能想到的唯一 bash 方法是检查是否存在匹配(不区分大小写),如果匹配,则从 $content 的开头排除适当数量的字符

content=foo_bar_baz
PRECEDING=FOO
shopt -s nocasematch
[[ $content == ${PRECEDING}* ]] && temp2=${content:${#PRECEDING}}
echo $temp2

Outputs: _bar_baz

输出: _bar_baz

回答by user8853220

your examples have context-switching techniques. better is (bash v4):

您的示例具有上下文切换技术。更好的是(bash v4):

VAR1="HELLoWORLD"
VAR2="hellOwOrld"

if [[ "${VAR1^^}" = "${VAR2^^}" ]]; then
    echo MATCH
fi

link: Converting string from uppercase to lowercase in Bash

链接:在 Bash 中将字符串从大写转换为小写

回答by Daniel Rich

If you don't have Bash 4, I find the easiest way is to first convert your string to lowercase using tr

如果您没有 Bash 4,我发现最简单的方法是首先使用将您的字符串转换为小写 tr

VAR1=HelloWorld
VAR2=helloworld

VAR1_LOWER=$(echo "$VAR1" | tr '[:upper:]' '[:lower:]')
VAR2_LOWER=$(echo "$VAR2" | tr '[:upper:]' '[:lower:]')

if [ "$VAR1_LOWER" = "$VAR2_LOWER" ]; then
  echo "Match"
else
  echo "Invalid"
fi

This also makes it really easy to assign your output to variables by changing your echo to OUTPUT="Match"& OUTPUT="Invalid"

这也使得通过将 echo 更改为OUTPUT="Match"&将输出分配给变量变得非常容易OUTPUT="Invalid"