Does anyone know if we can say set +x in bash without it being printed:
set -x
command
set +x
traces
+ command
This is a combination of a few ideas that can enclose a block of code and preserves the exit status.
#!/bin/bash
shopt -s expand_aliases
alias trace_on='set -x'
alias trace_off='{ PREV_STATUS=$? ; set +x; } 2>/dev/null; (exit $PREV_STATUS)'
trace_on
echo hello
trace_off
echo "status: $?"
trace_on
(exit 56)
trace_off
echo "status: $?"
When executed:
$ ./test.sh
+ echo hello
hello
status: 0
+ exit 56
status: 56