bash shell 脚本中的“=~”运算符有什么作用?

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

What does the "=~" operator do in shell scripts?

bashshell

提问by penartur

It seems that it is sort of comparison operator, but what exactly it does in e.g. the following code (taken from https://github.com/lvv/git-prompt/blob/master/git-prompt.sh#L154)?

似乎它是一种比较运算符,但是它在例如以下代码(取自https://github.com/lvv/git-prompt/blob/master/git-prompt.sh#L154)中究竟做了什么?

    if [[ $LC_CTYPE =~ "UTF" && $TERM != "linux" ]];  then
            elipses_marker="…"
    else
            elipses_marker="..."
    fi

I'm currently trying to make git-promptto work under MinGW, and the shell supplied with MinGW doesn't seem to support this operator:

我目前正在尝试git-prompt在 MinGW 下工作,MinGW 提供的 shell 似乎不支持这个操作符:

conditional binary operator expected
syntax error near `=~'
`        if [[ $LC_CTYPE =~ "UTF" && $TERM != "linux" ]];  then'

In this specific case I can just replace the entire block with elipses_marker="…"(as I know my terminal supports unicode), but what exactly this =~does?

在这种特定情况下,我可以将整个块替换为elipses_marker="…"(因为我知道我的终端支持 unicode),但这究竟是=~做什么的?

采纳答案by MattBianco

It's a bash-only addition to the built-in [[command, performing regexp matching. Since it doesn't have to be an exact match of the full string, the symbol is waved, to indicate an "inexact" match.

它是对内置[[命令的仅 bash 补充,用于执行正则表达式匹配。由于它不必是完整字符串的精确匹配,因此会挥动符号,以表示“不精确”匹配。

In this case, if $LC_CTYPECONTAINSthe string "UTF".

在这种情况下,如果$LC_CTYPECONTAINS字符串“UTF”。

More portable version:

更便携的版本:

if test `echo $LC_CTYPE | grep -c UTF` -ne 0 -a "$TERM" != "linux"
then
  ...
else
  ...
fi

回答by Micha? Górny

It's a regular expression matching. I guess your bash version doesn't support that yet.

这是一个正则表达式匹配。我猜你的 bash 版本还不支持。

In this particular case, I'd suggest replacing it with simpler (and faster) pattern matching:

在这种特殊情况下,我建议用更简单(更快)的模式匹配替换它:

[[ $LC_CTYPE == *UTF* && $TERM != "linux" ]]

(note that *must not be quoted here)

(注意*这里不能引用)

回答by alex

Like Ruby, it matches where the RHS operand is a regular expression.

与 Ruby 一样,它匹配 RHS 操作数是正则表达式的地方。

回答by Rozuur

It matches regular expressions

它匹配正则表达式

Refer to following example from http://tldp.org/LDP/abs/html/bashver3.html#REGEXMATCHREF

请参阅http://tldp.org/LDP/abs/html/bashver3.html#REGEXMATCHREF 中的以下示例

#!/bin/bash

input=


if [[ "$input" =~ "[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]" ]]
#                 ^ NOTE: Quoting not necessary, as of version 3.2 of Bash.
# NNN-NN-NNNN (where each N is a digit).
then
  echo "Social Security number."
  # Process SSN.
else
  echo "Not a Social Security number!"
  # Or, ask for corrected input.
fi