在我的命令行中,为什么 echo $0 返回“-”?

发布于 2024-12-20 14:24:01 字数 111 浏览 0 评论 0原文

当我输入 echo $0 时,我看到 -

我希望看到 bash 或某些文件名,如果我只得到 "-" 这意味着什么?

When I type echo $0 I see -

I expect to see bash or some filename, what does it mean if I just get a "-"?

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

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

发布评论

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

评论(3

无声静候 2024-12-27 14:24:01

$0 前面的连字符表示该程序是登录 shell。

注意:$0 并不总是包含正在运行的可执行文件的准确路径,因为在调用 execve(2) 时有一种方法可以覆盖它。

A hyphen in front of $0 means that this program is a login shell.

note: $0 does not always contain accurate path to the running executable as there is a way to override it when calling execve(2).

小帐篷 2024-12-27 14:24:01

我得到“-bash”,几周前,我尝试修改运行 pstop/htopecho $0 时可见的进程名称代码>.直接回答你的问题,我认为这没有任何意义。 Echo 是 bash 的内置函数,因此当它检查参数列表时,bash 实际上正在执行检查,并在那里看到自己。

您的直觉是正确的,如果您在脚本文件中写入 echo $0 并运行它,您将看到脚本的文件名。

I get '-bash', a few weeks ago, I played with modifying a process name visible when you run ps or top/htop or echo $0. To answer you question directly, I don't think it means anything. Echo is a built-in function of bash, so when it checks the arguments list, bash is actually doing the checking, and seeing itself there.

Your intuition is correct, if you wrote echo $0 in a script file, and ran that, you would see the script's filename.

恍梦境° 2024-12-27 14:24:01

因此,根据您的评论之一,您确实想知道如何确定您正在运行的 shell;您认为 $0 是解决方案,并询问了这一点,但正如您所见,$0 不会可靠地告诉您需要知道的内容。

如果您正在运行 bash,则会设置几个未导出的变量,包括 $BASH_VERSION。如果您正在运行 tcsh,则将设置 shell 变量 $tcsh$version。 (请注意,$version 是一个过于通用的名称;我遇到过一些系统范围的启动脚本设置它并破坏 tcsh 特定变量的问题。但是 $tcsh > 应该是可靠的。)

但真正的问题是 bash 和 tcsh 语法大多不兼容。也许可以编写一个可以在从 tcsh 或 bash 调用(通过 .source)时执行的脚本,但这会很困难且难看。

通常的方法是拥有单独的安装文件,每个文件对应您使用的每个 shell。例如,如果您正在运行 bash,您可能会运行

. ~/setup.bash

or

. ~/setup.sh

;如果您正在运行 tcsh,您可能会运行

source ~/setup.tcsh

or。

source ~/setup.csh

.sh.csh 版本指的是两个壳;如果您不使用任何特定于 bash 或 tcsh 的功能,那么使用这些后缀是有意义的。

但这需要知道您正在运行哪个 shell。

您可以在 .cshrc.tcshrc 或.login中设置别名,并在.profile< 中设置别名或函数code>、.bash_profile.bashrc` 将调用您需要的任何脚本。

或者,如果您想在每次登录或每次启动新的交互式 shell 时进行设置,您可以将命令直接放入相应的 shell 启动文件中。当然,tcshbash 的命令会有所不同。

So based on one of your comments, you're really want to know how to determine what shell you're running; you assumed $0 was the solution, and asked about that, but as you've seen $0 won't reliably tell you what you need to know.

If you're running bash, then several unexported variables will be set, including $BASH_VERSION. If you're running tcsh, then the shell variables $tcsh and $version will be set. (Note that $version is an excessively generic name; I've run into problems where some system-wide startup script sets it and clobbers the tcsh-specific variable. But $tcsh should be reliable.)

The real problem, though, is that bash and tcsh syntax are mostly incompatible. It might be possible to write a script that can execute when invoked (via . or source) from either tcsh or bash, but it would be difficult and ugly.

The usual approach is to have separate setup files, one for each shell you use. For example, if you're running bash you might run

. ~/setup.bash

or

. ~/setup.sh

and if you're running tcsh you might run

source ~/setup.tcsh

or

source ~/setup.csh

The .sh or .csh versions refer to the ancestors of both shells; it makes sense to use those suffixes if you're not using any bash-specific or tcsh-specific features.

But that requires knowing which shell you're running.

You could probably set up an alias in your .cshrc, .tcshrc, or.login, and an alias or function in your.profile,.bash_profile, or.bashrc` that will invoke whichever script you need.

Or if you want to do the setup every time you login, or every time you start a new interactive shell, you can put the commands directly in the appropriate shell startup file(s). Of course the commands will be different for tcsh vs. bash.

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