如何确定我正在处理的当前 shell [英] How to determine the current shell I'm working on
问题描述
如何确定我正在处理的当前 shell?
How can I determine the current shell I am working on?
仅 ps
命令的输出就足够了吗?
Would the output of the ps
command alone be sufficient?
如何在不同风格的 Unix 中做到这一点?
How can this be done in different flavors of Unix?
推荐答案
有三种方法可以找到当前 shell 的可执行文件的名称:
请注意,如果 shell 的可执行文件是
/bin/sh
,则所有三种方法都可能被愚弄,但它实际上是一个重命名的bash
,例如(经常发生).Please note that all three approaches can be fooled if the executable of the shell is
/bin/sh
, but it's really a renamedbash
, for example (which frequently happens).因此,关于
ps
输出是否会执行的第二个问题的回答是不总是".Thus your second question of whether
ps
output will do is answered with "not always".echo $0
- 将打印程序名称...在 shell 的情况下是实际的 shell.
echo $0
- will print the program name... which in the case of the shell is the actual shell.
ps -ef |grep $$ |grep -v grep
- 这将在正在运行的进程列表中查找当前进程 ID.由于当前进程是shell,所以会被包含.ps -ef | grep $$ | grep -v grep
- this will look for the current process ID in the list of running processes. Since the current process is the shell, it will be included.这不是 100% 可靠的,因为您可能有 其他 进程的
ps
列表包含与 shell 的进程 ID 相同的数字,特别是如果该 ID 是一个小数字(例如,如果 shell 的 PID 为5",您可能会在同一个grep
输出中找到名为java5"或perl5"的进程!).这是ps"方法的第二个问题,除了不能依赖外壳名称.This is not 100% reliable, as you might have other processes whose
ps
listing includes the same number as shell's process ID, especially if that ID is a small number (for example, if the shell's PID is "5", you may find processes called "java5" or "perl5" in the samegrep
output!). This is the second problem with the "ps" approach, on top of not being able to rely on the shell name.echo $SHELL
- 当前 shell 的路径存储为任何 shell 的SHELL
变量.对此的警告是,如果您将 shell 作为子进程显式启动(例如,它不是您的登录 shell),您将获得登录 shell 的值.如果可能,请使用ps
或$0
方法.echo $SHELL
- The path to the current shell is stored as theSHELL
variable for any shell. The caveat for this one is that if you launch a shell explicitly as a subprocess (for example, it's not your login shell), you will get your login shell's value instead. If that's a possibility, use theps
or$0
approach.但是,如果可执行文件与您的实际 shell 不匹配(例如
/bin/sh
实际上是 bash 或 ksh),则您需要试探法.以下是一些特定于各种 shell 的环境变量:If, however, the executable doesn't match your actual shell (e.g.
/bin/sh
is actually bash or ksh), you need heuristics. Here are some environmental variables specific to various shells:$version
在 tcsh 上设置
$version
is set on tcsh
$BASH
在 bash 上设置$BASH
is set on bash$shell
(小写)设置为 csh 或 tcsh 中的实际 shell 名称$shell
(lowercase) is set to actual shell name in csh or tcsh$ZSH_NAME
在 zsh 上设置ksh 设置了
$PS3
和$PS4
,而普通的 Bourne shell (sh
) 只有$PS1
和$PS2
设置.这通常似乎是最难区分的 - 我们在 Solaris boxen 上安装的sh
和ksh
之间的整个环境变量集的唯一区别是$ERRNO
、$FCEDIT
、$LINENO
、$PPID
、$PS3
、$PS4
、$RANDOM
、$SECONDS
和$TMOUT
.ksh has
$PS3
and$PS4
set, whereas the normal Bourne shell (sh
) only has$PS1
and$PS2
set. This generally seems like the hardest to distinguish - the only difference in the entire set of environment variables betweensh
andksh
we have installed on Solaris boxen is$ERRNO
,$FCEDIT
,$LINENO
,$PPID
,$PS3
,$PS4
,$RANDOM
,$SECONDS
, and$TMOUT
.这篇关于如何确定我正在处理的当前 shell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!