小编Ins*_*are的帖子

如何使用`local -`将shell选项限制为函数?

BASH 手册页local

local [option] [name[=value] ... | - ]
    […] If name is ‘-’, the set of shell options is made local to the 
    function in which local is invoked: shell options changed using the
    set builtin inside the function are restored to their original
    values when the function returns.
Run Code Online (Sandbox Code Playgroud)

因此,根据这些信息,使用local -,我应该能够set -x在函数内部使用来启用跟踪输出。当函数结束时,跟踪输出将被禁用。

#!/bin/bash
foo() {
  local -
  set -x
  ... # this code is shown in trace output
}

# trace output is …
Run Code Online (Sandbox Code Playgroud)

bash

6
推荐指数
1
解决办法
301
查看次数

标签 统计

bash ×1