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

查看:34
本文介绍了如何确定我正在处理的当前 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 - 将打印程序名称...在 shell 的情况下是实际的 shell.

    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.由于当前进程是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.

    这不是 100% 可靠的,因为您可能有 其他 进程的 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 的路径存储为任何 shell 的 SHELL 变量.对此的警告是,如果您将 shell 作为子进程显式启动(例如,它不是您的登录 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),则您需要试探法.以下是一些特定于各种 shell 的环境变量:


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

    • $version is set on tcsh

    $BASH 在 bash 上设置

    $BASH is set on 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 之间的整个环境变量集的唯一区别是 $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天全站免登陆