如何确定我正在使用的当前shell ?

仅仅ps命令的输出就足够了吗?

如何在不同风格的Unix中实现这一点?


当前回答

我特别喜欢Nahuel Fouilleul的解决方案,但我不得不在Ubuntu 18.04 (Bionic Beaver)上使用内置的Bash shell运行它的以下变体:

bash -c 'shellPID=$$; ps -ocomm= -q $shellPID'

没有临时变量shellPID,例如:

bash -c 'ps -ocomm= -q $$'

只会输出ps。也许你们不是都在使用非交互模式,这是有区别的。

其他回答

下面将始终给出实际使用的shell -它获得实际可执行文件的名称,而不是shell名称(即ksh93而不是ksh,等等)。对于/bin/sh,它将显示实际使用的shell,即破折号。

ls -l /proc/$$/exe | sed 's%.*/%%'

我知道有很多人说ls输出永远不应该被处理,但是您所使用的shell以特殊字符命名,或者位于以特殊字符命名的目录中的可能性有多大?如果情况仍然如此,还有很多其他不同的做法。

正如托比·斯佩特(Toby Speight)所指出的,这将是实现相同目标的更适当、更干净的方式:

basename $(readlink /proc/$$/exe)

不需要从"ps"的输出中Grepping PID,因为你可以从/proc目录结构中读取任何PID的相应命令行:

echo $(cat /proc/$$/cmdline)

然而,这可能并不比简单地:

echo $0

关于运行一个与名称不同的shell,一个想法是使用之前获得的名称从shell请求版本:

<some_shell> --version

sh似乎失败的退出码2,而其他人给出一些有用的(但我无法验证所有,因为我没有他们):

$ sh --version
sh: 0: Illegal option --
echo $?
2

我的解决方案:

ps -o command | grep -v -e "\<ps\>" -e grep -e tail | tail -1

这应该可以跨不同的平台和shell进行移植。它像其他解决方案一样使用ps,但它不依赖于sed或awk,并过滤掉管道和ps本身的垃圾,因此shell应该始终是最后一个条目。这样我们就不需要依赖不可移植的PID变量或选择正确的行和列。

我已经在Debian和macOS上用Bash、Z shell (zsh)和fish进行了测试(如果不专门为fish更改表达式,大多数解决方案都无法使用,因为它使用了不同的PID变量)。

There are three approaches to finding the name of the current shell's executable: Please note that all three approaches can be fooled if the executable of the shell is /bin/sh, but it's really a renamed bash, for example (which frequently happens). Thus your second question of whether ps output will do is answered with "not always". echo $0 - will print the program name... which in the case of the shell is the actual 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. 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 same grep output!). This is the second problem with the "ps" approach, on top of not being able to rely on the shell name. echo $SHELL - The path to the current shell is stored as the SHELL 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 the ps or $0 approach. 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 is set on tcsh $BASH is set on bash $shell (lowercase) is set to actual shell name in csh or tcsh $ZSH_NAME is set on zsh 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 between sh and ksh we have installed on Solaris boxen is $ERRNO, $FCEDIT, $LINENO, $PPID, $PS3, $PS4, $RANDOM, $SECONDS, and $TMOUT.

这个在Red Hat Linux (RHEL), macOS, BSD和一些aix上运行良好:

ps -T $$ | awk 'NR==2{print $NF}' 

或者,如果pstree可用,下面的一个也应该工作,

pstree | egrep $$ | awk 'NR==2{print $NF}'