The BASH man page for local says
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.
So based on this information, using local -, I should be able to use set -x inside of a function to enable trace output. When the function ends, the trace output would be disabled.
#!/bin/bash
foo() {
local -
set -x
... # this code is shown in trace output
}
# trace output is disabled
foo
# trace output should be disabled here
This does not work. When running the script, I get the error message
line 3: local: '-': not a valid identifier
and after the foo function returns, the trace output is still enabled for the remainder of the script.
I've tried this under BASH 4.4 on OSX and Ubuntu and BASH 3.2 on SLES.