隐藏xtrace中的命令

时间:2013-03-04 18:24:40

标签: linux bash

有没有办法使用xtrace(set -o xtrace)隐藏输出中的命令?

在DOS中,该技术是转向echo on,但使用@符号为隐藏命令添加前缀。

2 个答案:

答案 0 :(得分:4)

我从未在bash中看过类似的技巧,但你有时可以使用子shell暂时启用对你感兴趣的命令的跟踪:

echo "This command is not traced"
(
  set -x
  echo "This command is traced"
)
echo "No longer traced"

如果因为您想要修改环境而不适合子shell,则可以set -x然后set +x,但这会留下跟踪set +x命令的工件。

答案 1 :(得分:0)

你可以做这样的事情

# At start of script, or where xtrace may be enabled:
[[ "$SHELLOPTS" =~ xtrace ]] && xtrace_on=1

# Later
set +o xtrace
# Your untraced code here
[[ "$xtrace_on" ]] && set -o xtrace