如何确定我正在使用的当前shell ?
仅仅ps命令的输出就足够了吗?
如何在不同风格的Unix中实现这一点?
如何确定我正在使用的当前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。也许你们不是都在使用非交互模式,这是有区别的。
其他回答
这个在Red Hat Linux (RHEL), macOS, BSD和一些aix上运行良好:
ps -T $$ | awk 'NR==2{print $NF}'
或者,如果pstree可用,下面的一个也应该工作,
pstree | egrep $$ | awk 'NR==2{print $NF}'
请使用以下命令:
ps -p $$ | tail -1 | awk '{print $4}'
使用$SHELL环境变量获取它。简单的sed可以删除路径:
echo $SHELL | sed -E 's/^.*\/([aA-zZ]+$)/\1/g'
输出:
bash
它在macOS、Ubuntu和CentOS上进行了测试。
echo $$ # Gives the Parent Process ID
ps -ef | grep $$ | awk '{print $8}' # Use the PID to see what the process is.
你如何知道你当前的shell是什么?
如果你只是想确保用户正在使用Bash调用脚本:
if [ -z "$BASH" ]; then echo "Please run this script $0 with bash"; exit; fi
或参考
if [ -z "$BASH" ]; then exec bash $0 ; exit; fi