什么是" =〜"运营商在shell脚本呢? [英] What does the "=~" operator do in shell scripts?

查看:95
本文介绍了什么是" =〜"运营商在shell脚本呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是那种比较运算,但正是它在如确实以下code(从<一所href=\"https://github.com/lvv/git-prompt/blob/master/git-prompt.sh#L154\">https://github.com/lvv/git-prompt/blob/master/git-prompt.sh#L154)?

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)?

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

我目前正在试图使混帐提示的MinGW下工作,并与MinGW的提供的外壳似乎并不支持这个操作:

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

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

在这个特定的情况下,我可以只用 elipses_marker =更换整个块...(因为我知道我的终端支持UNI code),但究竟是什么这个 =〜呢?

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?

推荐答案

这是一个唯一的庆典,除了内置的 [命令,执行的正则表达式(模糊)匹配。

It's a bash-only addition to the built-in [[ command, performing regexp (fuzzy) matching.

在此情况下,如果 $ LC_CTYPE 包含的字符串UTF。

In this case, if $LC_CTYPE CONTAINS the string "UTF".

更多携带版:

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

这篇关于什么是&QUOT; =〜&QUOT;运营商在shell脚本呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆