Echo command, and then run it? (Like make)

2019-03-18 07:33发布

Is there some way to get bash into a sort of verbose mode where, such that, when it's running a shell script, it echoes out the command it's going to run before running it? That is, so that it's possible to see the commands that were run (as well as their output), similar to the output of make?

That is, if running a shell script like

echo "Hello, World"

I would like the following output

echo "Hello, World"
Hello, World

Alternatively, is it possible to write a bash function called echo_and_run that will output a command and then run it?

$ echo_and_run echo "Hello, World"
echo "Hello, World"
Hello, World

7条回答
Viruses.
2楼-- · 2019-03-18 08:07

Create executable(+x) base script named as "echo_and_run" with below mentioned simple shell script!

#!/bin/bash
echo "$1"
$1

$ ./echo_and_run "echo Hello, World"

echo Hello, World
Hello, World

However, cnicutar's approch to set -x is reliable and strongly recommended.

查看更多
登录 后发表回答