2013-03-04 93 views
3

有没有一种方法可以通过xtrace隐藏命令(set -o xtrace)?从xtrace隐藏命令

在DOS中,该技术将转为echo on,但以@符号为前缀隐藏的命令。

回答

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命令的文物。

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