avatar

ShīnChvën ✨

Effective Accelerationism

Powered by Druid

Print Commands in Bash Script

Sat Nov 24 2018

We can transcript and run multiple commands in one single bash script file, but it won't print the command you wrote in script by default. Whereas, printing commands in script is supported, it just needs to be enabled by adding The Set Builtin.

verbose

Print shell input lines as they are read.

Value of variables won't be print out, it's safe in this way to protect your secrets like password or token.

Use in Command Line

bash -v <YOUR_SCRIPT>

Use in Script

#!/usr/bin/env bash
set -v
## ... this rest of your scripts

xtrace

Print a trace of simple commands, for commands, case commands, select commands, and arithmetic for commands and their arguments or associated word lists after they are expanded and before they are executed. The value of the PS4 variable is expanded and the resultant value is printed before the command and its expanded arguments.

I do NOT recommend any one to use xtrace in production, for your secrets passed in variables like password or token might be cached in log or somewhere else, they might be leaked.

Use in Command Line

bash -x <YOUR_SCRIPT>

Use in Script

#!/usr/bin/env bash
set -x
## ... this rest of your scripts