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

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

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


当前回答

echo $$ # Gives the Parent Process ID 
ps -ef | grep $$ | awk '{print $8}' # Use the PID to see what the process is.

你如何知道你当前的shell是什么?

其他回答

$SHELL不需要总是显示当前SHELL。它只反映要调用的默认shell。

为了测试上面的测试,假设bash是默认shell,尝试echo $ shell,然后在同一终端,进入其他一些shell(例如KornShell (ksh))并尝试$ shell。在这两种情况下,您将看到结果为bash。

要获得当前shell的名称,使用cat /proc/$$/cmdline。和通过readlink /proc/$$/exe到shell可执行文件的路径。

我尝试过许多不同的方法,对我来说最好的方法是:

ps -p $$

它也可以在Cygwin下工作,并且不能像PID grepping那样产生假阳性。经过一些清理,它只输出一个可执行名称(在Cygwin With path下):

ps -p $$ | tail -1 | awk '{print $NF}'

你可以创建一个函数,这样你就不必记住它:

# Print currently active shell
shell () {
  ps -p $$ | tail -1 | awk '{print $NF}'
}

…然后执行shell。

它在Debian和Cygwin下进行了测试。

没有一个答案适用于fish shell(它没有变量$$或$0)。

这适用于我(测试在sh, bash, fish, ksh, csh, true, tcsh,和zsh;openSUSE 13.2):

ps | tail -n 4 | sed -E '2,$d;s/.* (.*)/\1/'

该命令输出类似bash的字符串。这里我只使用ps、tail和sed(没有GNU扩展;尝试添加——posix来检查它)。它们都是标准的POSIX命令。我相信尾巴是可以去掉的,但是我的sed fu还不够强。

在我看来,这个解决方案不是很可移植,因为它不能在OS x上工作:(

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.

一种方法是:

ps -p $$ -o exe=

在我看来,这比在另一个答案中使用-o args或-o comm更好(这些可能会使用一些符号链接,例如当/bin/sh指向某个特定的shell,如Dash或Bash)。

上面返回的是可执行文件的路径,但要注意,由于/usr-merge,可能需要检查多个路径(例如/bin/bash和/usr/bin/bash)。

还要注意,上面的文件并不完全与POSIX兼容(POSIX ps没有exe)。