set -x在bash中使用将shell扩展命令打印到stderr.我想将它们重定向到文件或管道.但不是整个输出 - 只有一些命令.就像是:
set -x command.txt ### <-- command.txt param is made up
echo $A $B
set +x
Run Code Online (Sandbox Code Playgroud)
这会将调试输出发送到命令.文本.
可以这样做吗?
Cyr*_*rus 19
随着bash4.1或更高版本:
#!/bin/bash
exec 5> command.txt
BASH_XTRACEFD="5"
echo -n "hello "
set -x
echo -n world
set +x
echo "!"
Run Code Online (Sandbox Code Playgroud)
输出到标准输出(FD 1):
hello world!
Run Code Online (Sandbox Code Playgroud)
输出到command.txt(FD 5):
+ echo -n world
+ set +x
Run Code Online (Sandbox Code Playgroud)