Mac OS X - bash 默认路径名扩展或错误

发布于 2024-09-29 23:19:02 字数 1095 浏览 8 评论 0原文

我正在尝试在 Mac OS X 10.6.4 上的 bash 中运行以下脚本行(来自 这个问题):

$ export EDITOR='mvim -f -c "au VimLeave * !open -a Terminal"'

唉,我得到的是意想不到的东西:

$ echo $EDITOR
mvim -f -c "au VimLeave 桌面文档下载库电影音乐图片公共站点 bin !open -a 终端"

预期输出为:

$ echo $EDITOR
mvim -f -c "au VimLeave * !open -a 终端"

解决这个问题的方法是设置noglob,即在export 赋值之前立即运行set -f。然而,眼前的问题是,这是否是 Mac OS X 上的预期行为,因为(因为默认情况下未设置 noglob,即 set +f),还是因为有一个Mac OS X 上 bash 中的错误

。bash 的版本是:

$ bash --version
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin10.0)
Copyright (C) 2007 Free Software Foundation, Inc.

Mac OS X 用户的 Unix 实用指南的第 329 页可能会提供一些帮助: “除非设置了 noglob(第 320 页),否则 shell 在遇到不明确的文件引用时会执行[路径名扩展]——包含任何未加引号的字符 &、?、[ 或 ] 的标记。”。然而,由于 * 被通配在引号内,所以问题仍然存在:该行为是 bash 的默认设置还是一个错误?

这只是一个好奇心,但我将不胜感激您的任何想法和意见。

布莱恩

I'm trying to run the following line of script in bash on Mac OS X 10.6.4 (from this question):

$ export EDITOR='mvim -f -c "au VimLeave * !open -a Terminal"'

Alas, what I get is something unexpected:

$ echo $EDITOR
mvim -f -c "au VimLeave Desktop Documents Downloads Library Movies Music Pictures Public Sites bin !open -a Terminal"

The expected output would be:

$ echo $EDITOR
mvim -f -c "au VimLeave * !open -a Terminal"

The way to fix this to this is to set noglob, i.e. run set -f immediately prior to the export assignment. However, the question at hand is whether this is the expected behaviour on Mac OS X because (because noglob is unset by default, i.e. set +f) or because there is a bug in bash on Mac OS X.

The version of bash is:

$ bash --version
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin10.0)
Copyright (C) 2007 Free Software Foundation, Inc.

There may be some assistance by way of page 329 of A practical guide to Unix for Mac OS X users: "Unless noglob (page 320) is set, the shell performs [pathname expansion] when it encounters an ambiguous file reference--a token containing any of the unquoted characters &, ?, [, or ].". However because the * being globbed is within quotes, the question remains: Is the behaviour a default setting of bash, or a bug?

This is just a curiosity, but I'd be grateful for any thoughts and input you may have.

Brian

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七度光 2024-10-06 23:19:02

您的 EDITOR 变量设置正确。如果执行以下命令,您可以看到这一点:

echo "$EDITOR"

看一下以下文字记录:


pax> export EDITOR='mvim -f -c "au VimLeave * !open -a Terminal"'

pax> echo $EDITOR
mvim -f -c "au VimLeave SecretCiaDoc.txt NsaEchelonKeys.txt !open -a Terminal"

pax> echo "$EDITOR"
mvim -f -c "au VimLeave * !open -a Terminal"

您的问题不在于 set 语句,而在于您的 echoset 不会扩展 *,因为它包含在单引号内,但执行不带引号的 echo 将扩展它。

这绝不会影响使用环境变量的程序。


根据您的评论:

这仍然很奇怪:* 仍然在 echo 命令的引号(双引号)内。 a='abc "*" xyz'; echo $a 不会在 bash 或 dash 中为我扩展;事实上,它包含引号作为第二个参数。

看看这个:

pax> a='abc "*" xyz' ; echo $a
abc "*" xyz

pax> a='abc "* xyz' ; echo $a
abc "* xyz

pax> a='abc " * xyz' ; echo $a
abc " SecretCiaDoc.txt NsaEchelonKeys.txt xyz

pax> touch '"hello' ; a='abc "* xyz' ; echo $a
abc "hello xyz

看看发生了什么。它不会将 " 视为任何特殊字符,而只是另一个字符。它为您的 EDITOR 扩展的原因是因为它是独立的。当您使用 "* “,它实际上是在尝试扩展以 ”开头和结尾的文件” - 您可以在上面的最后一个示例中看到这一点。

Your EDITOR variable is set correctly. You can see this if you execute:

echo "$EDITOR"

Have a look at the following transcript:


pax> export EDITOR='mvim -f -c "au VimLeave * !open -a Terminal"'

pax> echo $EDITOR
mvim -f -c "au VimLeave SecretCiaDoc.txt NsaEchelonKeys.txt !open -a Terminal"

pax> echo "$EDITOR"
mvim -f -c "au VimLeave * !open -a Terminal"

Your problem lies not with the set statement but with your echo. The set will not expand the * because it's contained within single quotes but doing an echo without quotes will expand it.

This in no way affects programs which use the environment variable.


Based on your comment:

This is still weird: that * is still within quotes (the double quotes) for the echo command. a='abc "*" xyz'; echo $a does not expand for me in either bash or dash; in fact, it includes the quotes as the second argument.

Watch this:

pax> a='abc "*" xyz' ; echo $a
abc "*" xyz

pax> a='abc "* xyz' ; echo $a
abc "* xyz

pax> a='abc " * xyz' ; echo $a
abc " SecretCiaDoc.txt NsaEchelonKeys.txt xyz

pax> touch '"hello' ; a='abc "* xyz' ; echo $a
abc "hello xyz

See what's happening. It's not treating the " as anything special, just another character. The reason why it expands for your EDITOR is because it's on its own. When you use "*", it's actually trying to expand files that begin and end with " - you can see that in my last example above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文