DEV Community

Discussion on: 101 Bash Commands and Tips for Beginners to Experts

Collapse
 
prennix profile image
Paul Rennix • Edited

WELL DONE!

set -x is a favorite of mine for debugging.

#!/bin/bash
set -x

this turns on line-by-line logic/command flow output and is great for debugging. It can be used inside a script or on the command line. If using on the command line, you'll want to turn it off at the end of the line. When used in a script, it's activity ends when the script does, or when unset with set -x

~ prennix$ set -x; for number in $(seq 1 3); \
do echo "Number ${number}"; done; \
if [ "${number}" == "3" ]; then echo "it's three"; \
else echo "this text is never printed"; fi; set +x

++ seq 1 3
+ for number in '$(seq 1 3)'
+ echo 'Number 1'
Number 1
+ for number in '$(seq 1 3)'
+ echo 'Number 2'
Number 2
+ for number in '$(seq 1 3)'
+ echo 'Number 3'
Number 3
+ '[' 3 == 3 ']'
+ echo 'it'\''s three'
it's three
+ set +x