如何确定我正在使用的当前shell [英] How to determine the current shell I'm working on

查看:85
本文介绍了如何确定我正在使用的当前shell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I determine the current shell I am working on?

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

Would the output of the ps command alone be sufficient?

如何在不同版本的Unix中完成此操作?

How can this be done in different flavors of Unix?

推荐答案

  • 有三种方法可以找到当前shell可执行文件的名称:

    请注意,如果shell的可执行文件为/bin/sh,则可以欺骗所有这三种方法,但是实际上,它实际上是重命名的bash(经常发生).

    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).

    因此,关于"ps"是否输出的第二个问题用"不总是"来回答.

    Thus your second question of whether ps output will do is answered with "not always".

    1. echo $0-将打印程序名称...在外壳程序的情况下为实际外壳程序.

    1. echo $0 - will print the program name... which in the case of the shell is the actual shell.

    ps -ef | grep $$ | grep -v grep-这将在正在运行的进程列表中查找当前进程ID.由于当前进程是外壳程序,因此将包含它.

    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.

    这不是100%可靠的,因为您可能有 other 个进程,这些进程的ps列表包含与Shell的进程ID相同的编号,尤其是在该ID很小的情况下(例如,如果如果Shell的PID为"5",则在同一grep输出中可能会找到名为"java5"或"perl5"的进程!). 这是"ps"方法的第二个问题,除了无法依赖外壳名称之外.

    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-当前外壳程序的路径存储为任何外壳程序的SHELL变量.需要注意的是,如果您显式启动外壳程序作为子进程(例如,它不是您的登录外壳程序),则将获得登录外壳程序的值.如果可能的话,请使用ps$0方法.

    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.


  • 但是,如果可执行文件与您的实际shell不匹配(例如/bin/sh实际上是bash或ksh),则需要启发式.以下是一些特定于各种外壳的环境变量:


  • 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在tcsh上设置

    $BASH被设置为bash

    $shell(小写)设置为csh或tcsh中的实际shell名称

    $shell (lowercase) is set to actual shell name in csh or tcsh

    $ZSH_NAME在zsh上设置

    ksh设置了$PS3$PS4,而普通的Bourne shell(sh)仅设置了$PS1$PS2.通常,这似乎很难区分-我们在Solaris boxen上安装的shksh之间的整个环境变量集中的 only 差异是$ERRNO$FCEDIT$LINENO$PPID$PS3$PS4$RANDOM$SECONDS$TMOUT.

    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.

    这篇关于如何确定我正在使用的当前shell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆