我如何知道我是否正在运行嵌套外壳程序? [英] How do I know if I'm running a nested shell?

查看:105
本文介绍了我如何知道我是否正在运行嵌套外壳程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用* nix shell(通常是bash)时,我经常产生一个子shell,可以用它来处理一个小任务(通常在另一个目录中),然后退出以继续执行父shell的会话

When using a *nix shell (usually bash), I often spawn a sub-shell with which I can take care of a small task (usually in another directory), then exit out of to resume the session of the parent shell.

有时,我会失去对运行嵌套shell还是在顶层shell中的跟踪,并且会意外生成另一个子shell或退出顶层外壳错误.

Once in a while, I'll lose track of whether I'm running a nested shell, or in my top-level shell, and I'll accidentally spawn an additional sub-shell or exit out of the top-level shell by mistake.

有没有一种简单的方法来确定我是否在嵌套shell中运行?还是我以完全错误的方式解决问题(通过生成子壳)?

Is there a simple way to determine whether I'm running in a nested shell? Or am I going about my problem (by spawning sub-shells) in a completely wrong way?

推荐答案

$SHLVL变量跟踪您的Shell嵌套级别:

The $SHLVL variable tracks your shell nesting level:

$ echo $SHLVL
1
$ bash
$ echo $SHLVL
2
$ exit
$ echo $SHLVL
1


作为生成子shell的替代方法,您可以从堆栈中推送和弹出目录并保留在同一shell中:


As an alternative to spawning sub-shells you could push and pop directories from the stack and stay in the same shell:

[root@localhost /old/dir]# pushd /new/dir
/new/dir /old/dir
[root@localhost /new/dir]# popd
/old/dir
[root@localhost /old/dir]#

这篇关于我如何知道我是否正在运行嵌套外壳程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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