如何确定我正在使用的当前shell ?
仅仅ps命令的输出就足够了吗?
如何在不同风格的Unix中实现这一点?
如何确定我正在使用的当前shell ?
仅仅ps命令的输出就足够了吗?
如何在不同风格的Unix中实现这一点?
当前回答
如果你的/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}'
其他回答
你可以使用echo $SHELL|sed "s/\/bin\// g"
$SHELL不需要总是显示当前SHELL。它只反映要调用的默认shell。
为了测试上面的测试,假设bash是默认shell,尝试echo $ shell,然后在同一终端,进入其他一些shell(例如KornShell (ksh))并尝试$ shell。在这两种情况下,您将看到结果为bash。
要获得当前shell的名称,使用cat /proc/$$/cmdline。和通过readlink /proc/$$/exe到shell可执行文件的路径。
不需要从"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
这不是一个非常干净的解决方案,但它是你想要的。
# MUST BE SOURCED..
getshell() {
local shell="`ps -p $$ | tail -1 | awk '{print $4}'`"
shells_array=(
# It is important that the shells are listed in descending order of their name length.
pdksh
bash dash mksh
zsh ksh
sh
)
local suited=false
for i in ${shells_array[*]}; do
if ! [ -z `printf $shell | grep $i` ] && ! $suited; then
shell=$i
suited=true
fi
done
echo $shell
}
getshell
现在您可以使用$(getshell)——version。
不过,这只适用于kornshell类shell (ksh)。
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.