Mac OS X - bash 默认路径名扩展或错误
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
EDITOR
变量设置正确。如果执行以下命令,您可以看到这一点:看一下以下文字记录:
您的问题不在于
set
语句,而在于您的echo
。set
不会扩展*
,因为它包含在单引号内,但执行不带引号的echo
将扩展它。这绝不会影响使用环境变量的程序。
根据您的评论:
看看这个:
看看发生了什么。它不会将
"
视为任何特殊字符,而只是另一个字符。它为您的EDITOR
扩展的原因是因为它是独立的。当您使用"* “
,它实际上是在尝试扩展以”开头和结尾的文件”
- 您可以在上面的最后一个示例中看到这一点。Your
EDITOR
variable is set correctly. You can see this if you execute:Have a look at the following transcript:
Your problem lies not with the
set
statement but with yourecho
. Theset
will not expand the*
because it's contained within single quotes but doing anecho
without quotes will expand it.This in no way affects programs which use the environment variable.
Based on your comment:
Watch this:
See what's happening. It's not treating the
"
as anything special, just another character. The reason why it expands for yourEDITOR
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.