微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

linux – 设置位置参数时使用 – 和 – 作为选项之间的区别

在man bash中提到set有两个选项 – 和 –

我想知道在设置位置参数时使用 – 和 – 作为选项时是否存在任何差异.

当我们在设置位置参数时使用它时,我找不到man bash中提到的任何重大差异.

解决方法:

4.1.5(1)的bash(1)手册页说:

--      If no arguments follow this option, then the  positional
        parameters are unset.  Otherwise, the positional parame‐
        ters are set to the args, even if  some  of  them  begin
        with a -.
-       Signal  the  end of options, cause all remaining args to
        be assigned to the positional parameters.  The -x and -v
        options are turned off.  If there are no args, the posi‐
        tional parameters remain unchanged.

一个区别是 – 或 – 之后没有参数.对于前者,现有的位置参数将保持不变.对于后者,将清除位置参数.

所以设置 – 清除位置参数和设置 – 是一个无操作.

-v和-x设置可以通过set修改 – ….所以,如果你设置-v打开(导致shell在读取时打印输入行),它将被设置关闭 – …命令.设置 – …将保持不变.

set -x更常见的是set -v在set -x中经常用于调试脚本以确切地查看正在运行的命令.通常在调试shell脚本时,您可以使用bash -x< script>运行它.知道set – …关闭-x,你可能想要使用set – …,因为将-x作为另一个命令的副作用关闭会非常意外.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐