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

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

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


当前回答

我的解决方案:

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变量)。

其他回答

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

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下进行了测试。

如果您只是想检查您正在运行(特定版本的)Bash,最好的方法是使用$BASH_VERSINFO数组变量。作为一个(只读)数组变量,它不能在环境中设置, 因此,您可以确定它来自当前shell(如果有的话)。

但是,由于Bash在作为sh调用时具有不同的行为,因此还需要检查以/ Bash结尾的$ Bash环境变量。

在我写的一个脚本中,使用了带-(不是下划线)的函数名,并依赖于关联数组(在Bash 4中添加),我有以下完整性检查(有有用的用户错误消息):

case `eval 'echo $BASH@${BASH_VERSINFO[0]}' 2>/dev/null` in
    */bash@[456789])
        # Claims bash version 4+, check for func-names and associative arrays
        if ! eval "declare -A _ARRAY && func-name() { :; }" 2>/dev/null; then
            echo >&2 "bash $BASH_VERSION is not supported (not really bash?)"
            exit 1
        fi
        ;;
    */bash@[123])
        echo >&2 "bash $BASH_VERSION is not supported (version 4+ required)"
        exit 1
        ;;
    *)
        echo >&2 "This script requires BASH (version 4+) - not regular sh"
        echo >&2 "Re-run as \"bash $CMD\" for proper operation"
        exit 1
        ;;
esac

在第一种情况下,您可以忽略对特性的功能检查,只假设将来的Bash版本是兼容的。

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.

如果你的/bin/sh支持POSIX标准,并且你的系统已经安装了lsof命令——在这种情况下,lsof的一个可能的替代方案可能是pid2path——你也可以使用(或改编)下面的脚本打印完整的路径:

#!/bin/sh
# cat /usr/local/bin/cursh
set -eu
pid="$$"

set -- sh bash zsh ksh ash dash csh tcsh pdksh mksh fish psh rc scsh bournesh wish Wish login

unset echo env sed ps lsof awk getconf

# getconf _POSIX_VERSION  # reliable test for availability of POSIX system?
PATH="`PATH=/usr/bin:/bin:/usr/sbin:/sbin getconf PATH`"
[ $? -ne 0 ] && { echo "'getconf PATH' failed"; exit 1; }
export PATH

cmd="lsof"
env -i PATH="${PATH}" type "$cmd" 1>/dev/null 2>&1 || { echo "$cmd not found"; exit 1; }

awkstr="`echo "$@" | sed 's/\([^ ]\{1,\}\)/|\/\1/g; s/ /$/g' | sed 's/^|//; s/$/$/'`"

ppid="`env -i PATH="${PATH}" ps -p $pid -o ppid=`"
[ "${ppid}"X = ""X ] && { echo "no ppid found"; exit 1; }

lsofstr="`lsof -p $ppid`" || 
   { printf "%s\n" "lsof failed" "try: sudo lsof -p \`ps -p \$\$ -o ppid=\`"; exit 1; }

printf "%s\n" "${lsofstr}" | 
   LC_ALL=C awk -v var="${awkstr}" '$NF ~ var {print $NF}'

我有一个简单的技巧来找到当前的壳。只需输入一个随机字符串(这不是命令)。它将失败并返回一个“not found”错误,但在行开始时,它会说它是哪个shell:

ksh: aaaaa: not found [No such file or directory]
bash: aaaaa: command not found