gpt4 book ai didi

linux - 我如何知道我是否在运行嵌套 shell?

转载 作者:IT老高 更新时间:2023-10-28 12:32:09 27 4
gpt4 key购买 nike

当使用 *nix shell(通常是 bash)时,我经常生成一个子 shell,我可以用它来处理一个小任务(通常在另一个目录中),然后退出以恢复父 shell 的 session .

有时,我会忘记我是在运行嵌套 shell 还是在顶级 shell 中,并且我会不小心生成一个额外的子 shell 或退出顶级 shell错误的shell。

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

最佳答案

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

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

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

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

关于linux - 我如何知道我是否在运行嵌套 shell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4511407/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com