Bash =〜在cmd提示符OS X上丢失BASH_REMATCH内容 [英] Bash =~ loosing BASH_REMATCH contents at the cmd prompt OS X
问题描述
假设我有一个(愚蠢的示例)脚本:
Suppose I have a (silly example) script:
#!/bin/bash
st="one two three"
[[ $st =~ ^([[:alpha:]]+)[[:space:]]([[:alpha:]]+)[[:space:]]([[:alpha:]]+) ]]
for i in "${BASH_REMATCH[@]}"
do
echo "$i"
done
它按预期方式工作-打印:
It works as expected -- it prints:
one two three
one
two
three
然后是每个比赛组的整体比赛.但是,如果我进入shell并输入:
Which is the overall match then each match group. HOWEVER, if I go to the shell and type:
$ st="one two three"
$ [[ $st =~ ^([[:alpha:]]+)[[:space:]]([[:alpha:]]+)[[:space:]]([[:alpha:]]+) ]]
$ for i in "${BASH_REMATCH[@]}"
> do
> echo "$i"
> done
它打印:
w
如果我这样做
$ [[ $st =~ ^([[:alpha:]]+)[[:space:]]([[:alpha:]]+)[[:space:]]([[:alpha:]]+) ]] && arr=( "${BASH_REMATCH[@]}" )
然后,我可以遍历 arr
中的 BASH_REMATCH
的副本,显示匹配正常.但是在Bash 3.2上使用交互式shell时, BASH_REMATCH
似乎发生了某些事情.比赛成功后.
I can then loop through the copy of BASH_REMATCH
in arr
showing that the match works OK. But something seems to happen to BASH_REMATCH
in the interactive shell use on Bash 3.2. right after a successful match.
这是macOS Sierra 10.12.1上的默认 3.2.57(1)-发行版
This is the default 3.2.57(1)-release
on macOS Sierra 10.12.1
如果我启动Bash 4.4,一切正常.
If I fire up Bash 4.4, it all works OK.
想法?
编辑
戈登·戴维斯是正确的:它与使用"$ {BASH_REMATCH [@]}"
的Apple实用程序有关,作为显示提示的一部分.
Gordon Davis is correct: it has to do with an Apple utility using "${BASH_REMATCH[@]}"
as part of displaying the prompt.
易于演示:
$ cd ~
$ pwd && echo "${BASH_REMATCH[@]}"
/Users/andrew
w
$ cd /tmp
$ pwd && echo "${BASH_REMATCH[@]}"
/tmp
p
然后,如果我运行的苹果bash没有 rc
文件:
Then if I run Apple's bash with no rc
file:
$ /bin/bash --norc
bash-3.2$ pwd && echo "${BASH_REMATCH[@]}"
/tmp
bash-3.2$
(那里"$ {BASH_REMATCH [@]}"
的空白行...)
(There is a blank line for "${BASH_REMATCH[@]}"
there...)
推荐答案
之所以会发生这种情况,是因为Apple的默认bash初始化文件具有精美的功能来定义提示,并且每次需要显示提示时都会重新计算该提示,并且该函数使用 =〜
,因此替换了 BASH_REMATCH
的先前内容.参见/etc/bashrc_Apple_Terminal(由/etc/bashrc派生)中的 update_terminal_cwd
的定义.
This happens because Apple's default bash init file has a fancy function to define the prompt, and it recomputes it every time the prompt needs to be displayed, and that function uses =~
and therefore replaces the previous contents of BASH_REMATCH
. See the definition of update_terminal_cwd
in /etc/bashrc_Apple_Terminal (which is sourced by /etc/bashrc).
如果这给您带来麻烦,我认为使用 unset PROMPT_COMMAND
取消定义该函数是安全的(该函数保留了定义,但未使用).
If this is causing you trouble, I think it's safe to undefine the function with unset PROMPT_COMMAND
(that leaves the function defined, but it is not used).
BTW, BASH_REMATCH
最终被设置为"w",因为该功能逐个字符地遍历您的工作目录,并对它们进行URL编码."w"显然是您工作目录中的最后一个字符.
BTW, BASH_REMATCH
wound up being set to "w" because the function steps through your working directory character by character, URL-encoding them as it goes. "w" is apparently the last character in your working directory.
查看该函数,它实际上对提示没有任何作用;它设置终端"窗口标题栏中的文件夹部分.如果您取消设置PROMPT_COMMAND,它将停止更新窗口标题.
looking at the function, it doesn't actually do anything with the prompt; it sets the folder section of the Terminal window title bar. If you unset PROMPT_COMMAND, it'll stop updating the window title.
这篇关于Bash =〜在cmd提示符OS X上丢失BASH_REMATCH内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!