如何在执行之前打印每个命令? [英] How can I print each command before executing?

查看:92
本文介绍了如何在执行之前打印每个命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置Bash脚本以在执行每个命令之前将其打印出来的最佳方法是什么?

What is the best way to set up a Bash script that prints each command before it executes it?

这对于调试目的非常有用.

That would be great for debugging purposes.

我已经尝试过了:

CMD="./my-command --params >stdout.txt 2>stderr.txt"
echo $CMD
`$CMD`

应该首先打印出来:

./my-command --params >stdout.txt 2>stderr.txt

然后执行./my-command --params,将输出重定向到指定的文件.

And then execute ./my-command --params, with the output redirected to the files specified.

推荐答案

set -o xtrace

bash -x myscript.sh

这也适用于标准的/bin/sh和IIRC(然后可能是POSIX)

记住,这里有 bashdb (bash Shell Debugger, release 4.0-0.4)

要恢复正常,请退出子Shell或

To revert to normal, exit the subshell or

set +o xtrace

这篇关于如何在执行之前打印每个命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆