加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux变量$BASH_SUBSHELL与$SHLVL之间的区别

发布时间:2020-12-13 16:56:51 所属栏目:Linux 来源:网络整理
导读:我在两者之间感到困惑. While the $BASH_SUBSHELL internal variable indicates the nesting level of a subshell,the $SHLVL variable shows no change within a subshell. 它究竟意味着什么?如果我在另一个shell中打开一个shell,$SHLVL的值会递增.不是那
我在两者之间感到困惑.

While the $BASH_SUBSHELL internal variable indicates the nesting level of a subshell,the $SHLVL variable shows no change within a subshell.

它究竟意味着什么?如果我在另一个shell中打开一个shell,$SHLVL的值会递增.不是那个子壳吗?

解决方法

不,手动运行新shell(通过/ bin / sh或/ bin / bash等)不是此上下文中的子shell.

子shell是指shell自己生成一个新的shell实例来处理某些工作.

使用Command Substitution(即$(命令))是一个子shell(就像旧的反引号调用一样).

使用pipeline(即echo’5.1 5.3’| bc -l)为管道的每个组件创建子壳.

使用Process Substitution(即<(command))创建子shell. Grouping commands(即(声明a = 5; echo $a))创建一个子shell.

在the background中运行命令(即睡眠1和1)创建子壳.

可能还有其他事情,但这些是常见的情况.

测试这很容易:

$printf "Outside: $BASH_SUBSHELL,$SHLVLnInside: $(echo $BASH_SUBSHELL,$SHLVL)n"
Outside: 0,1
Inside: 1,1
$(printf "Outside: $BASH_SUBSHELL,$SHLVL)n")
Outside: 1,1
Inside: 2,1
$bash -c 'printf "Outside: $BASH_SUBSHELL,$SHLVL)n"'
Outside: 0,2
Inside: 1,2
$bash -c '(printf "Outside: $BASH_SUBSHELL,$SHLVL)n")'
Outside: 1,2
Inside: 2,2

您的报价来源(通常很差,通常更好地避免,ABS)甚至可以证明这一点(并且以一种相当不清楚的方式,只是“高级”指南中普遍缺乏严谨性和质量的另一个例子):

06001

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读