Bash 错误消息有时带有-bash:
或bash:
前缀,有时没有。
考虑来自 Ubuntu 14.04.5 Trusty Tahr with bash 4.3.11(1) 的这些错误消息
$ type encabulator
-bash: type: encabulator: not found
$ encabulator
encabulator: command not found
$ bash -c encabulator
bash: encabulator: command not found
Run Code Online (Sandbox Code Playgroud)
在另一个问题中,有人报告说看到了一条消息
-bash: fetch: command not found.
Run Code Online (Sandbox Code Playgroud)
当“fetch”只是一个尚未安装的程序时,这与预期的模式不匹配。
在下面的评论之后,我尝试在 Centos 5.7 上使用 bash 3.2.25(1) - 它的行为略有不同
$ type encabulator
-bash: type: encabulator: not found
$ encabulator
-bash: encabulator: command not found
$ bash -c encabulator
bash: encabulator: command not found
Run Code Online (Sandbox Code Playgroud)
所以我想来自另一个问题的消息正是我最初的想法,在我对 bash 4.3.11 的测试让我感到困惑之前。
bash 维护者是否打算将一些一致的系统应用于这些文本消息的结构,这仍然是一个悬而未决的问题?
bash 何时在错误消息前加上 with-bash:
和何时加上bash:
(前导连字符表示什么?)
我无法使用简单的搜索找到答案,man bash
但也许有一个部分提供了解释?
bash 前缀错误消息的确切时间是 with
-bash:
以及何时 withbash:
前缀(如果打印)通常是 bash 启动的名称,也就是argv[0]
(通常在 中可用$0
)。
$ bash -c 'type foo; echo $0'
bash: line 0: type: foo: not found
bash
$ ARGV0=bar bash -c 'type foo; echo $0'
bar: line 0: type: foo: not found
bar
$ ARGV0='¯\(°_o)/¯' bash -c 'type foo; echo $0'
¯\(°_o)/¯: line 0: type: foo: not found
¯\(°_o)/¯
Run Code Online (Sandbox Code Playgroud)
(这使用zsh 的特殊 ARGV0 变量将argv[0]
命令设置为我们想要的任何内容。)
对于启动bash作为登录shell,无论开始的bash可以前缀argv[0]
与-
,或使用-l
选项。请参阅调用 Bash:
登录 shell 是一个参数零的第一个字符是 '
-
' 的,或者是用该--login
选项调用的。
所以,例如,
$ ARGV0=-bash bash -c 'echo -n $0:;shopt -q login_shell && echo login || echo not login'
-bash:login
$ bash -c 'echo -n $0:;shopt -q login_shell && echo login || echo not login'
bash:not login
Run Code Online (Sandbox Code Playgroud)
SSH, sudo
(with -i
), TTYlogin
命令等通常使用领先的-
方法来启动登录shell。因此,如果您通过其中任何一个登录,您很可能会看到-bash
(或-zsh
、 或-tcsh
或您登录 shell-
前面的任何内容)。如果您通过终端模拟器启动 bash,那些通常运行非登录 shell,您会看到bash
.
前导连字符表示什么?
它可能是一个登录shell,这可能是调试中的一个有用点。特别是,PATH
往往是由登录shell源文件中设置(/etc/profile
,~/.profile
,~/.bash_profile
,等等-见巴什启动文件更多)。对于未找到命令的错误,这尤其重要,因为PATH
这是找到命令的方式。它为您提供有关应检查哪些文件进行修改的有用信息PATH
。
bash 到底什么时候在错误消息前加上 -bash: 以及什么时候加上 bash:
这是任意的。
前导连字符表示什么?
没什么用处。请参阅muru 的答案,其中指出连字符表示登录 shell正在报告错误消息。
Bash 的GNU手册提到了 shell 选项
gnu_errfmt
If set, shell error messages are written in the standard GNU error message format.
Run Code Online (Sandbox Code Playgroud)
我尝试设置它,问题中的结果没有改变。
GNU 编码标准似乎主要关注来自编译器的错误消息(即gcc
?),但是第 4.4 节说
4.4 Formatting Error Messages
...
... like this:
program: message
when there is no relevant source file.
Run Code Online (Sandbox Code Playgroud)
GNU 的libc 手册给出了生成错误消息的示例。
fprintf (stderr, "%s: Couldn't open file %s; %s\n",
program_invocation_short_name, name, strerror (errno));
exit (EXIT_FAILURE);
Run Code Online (Sandbox Code Playgroud)
根据上述文档中缺乏细节以及问题中看到的变化,我推断前缀的存在或不存在没有特定的含义bash:
。
归档时间: |
|
查看次数: |
414 次 |
最近记录: |