🐚 Bash Logging & Debugging with Debug Mode

Writing shell scripts can get tricky, so knowing how to log output and debug your scripts is 🔑.

📝 Logging Output

Redirect standard output (stdout) and error (stderr) to log files:


# Redirect stdout to file
echo "Hello, log!" > output.log

# Redirect stderr to file
ls non_existent_file 2> error.log

# Redirect both stdout and stderr to the same file
command &> all_output.log
  

🐞 Debugging Tips

Simulate bash -x, set -x, set +x to see commands traced during execution.

📝 Try a script below:

Output will appear here...
💡 Tip: Use set -x to turn ON debug mode and set +x to turn it OFF inside your script.