Adding comments using 'set -x'
One hacky way is just to write your comments as arguments to a no-op command. Particularly useful might be the :
null utility:
set -x
: Some interesting notes on the following are ...
results in:
+ : Some interesting notes on the following are...
The colon command does nothing, accepts whatever arguments you give it, and always succeeds. You get an extra :
at the start of your trace output, but that probably isn't a huge problem for your purpose.
If you don't like the :
an even nastier trick is to use a fake command:
set -x
seq 1 1
Some comment &>/dev/null
true
will output:
+ seq 1 1
1
+ Some comment
+ true
That is, the Some comment
line is printed out as trace output when the shell tries to run it, but the resulting error message is sent to /dev/null
. This is nasty for a lot of obvious reasons, but it also counts as an error for the purposes of set -e
.
Note that in either case, your comment is parsed by the shell in the ordinary way, so in particular if you have any special characters they need to be quoted, and because it's trace output the quoting will be displayed.
Instead of using "set -x" to print out the debug information, use a separate debug function:
#!/bin/bash
DEBUG=1
debug() {
if [ $DEBUG == 1 ]
then
echo "DEBUG:" $@
fi
}
debug "foo bar"
Running the script yields
temeraire:ul jenny$ ./testdebug.sh
DEBUG: foo bar
If you change the assignment to instead read DEBUG=0
, it won't print out the line.
I use set -x
to debug scripts all the time, and have only ever found the solution that @MichaelHomer suggests which makes use of the null utility (aka. : ..some comment...).
When you're ready to move to a more potent solution I'd suggest implementing your scripts using and actual logger such as log4Bash
.
Example
#!/usr/bin/env bash
source log4bash.sh
log "This is regular log message... log and log_info do the same thing";
log_warning "Luke ... you turned off your targeting computer";
log_info "I have you now!";
log_success "You're all clear kid, now let's blow this thing and go home.";
log_error "One thing's for sure, we're all gonna be a lot thinner.";
# If you have figlet installed -- you'll see some big letters on the screen!
log_captains "What was in the captain's toilet?";
# If you have the "say" command (e.g. on a Mac)
log_speak "Resistance is futile";
Results: