在bash中,如何获取set -x的当前状态? [英] In bash, how to get the current status of set -x?

查看:80
本文介绍了在bash中,如何获取set -x的当前状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在脚本中临时设置-x,然后返回到原始状态。

I would like to set -x temporarily in my script and then return in to the original state.

有没有一种方法可以在不启动新子shell的情况下进行操作?
之类的东西

Is there a way to do it without starting new subshell? Something like

echo_was_on=.......
... ...
if $echo_was_on; then set -x; else set +x; fi


推荐答案

您可以检查<$ c $的值c> $-查看当前选项;如果它包含x,则设置为x。您可以这样检查:

You can check the value of $- to see the current options; if it contains an x, it was set. You can check like so:

old_setting=${-//[^x]/}
...
if [[ -n "$old_setting" ]]; then set -x; else set +x; fi

这篇关于在bash中,如何获取set -x的当前状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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