see each command a script runs
Debug a script: print every command as it runs, check its syntax, find common mistakes.
Steps
-
bash -x script.shrun it, printing each command first
-
set -xinside the script: print from here on; set +x stops
-
bash -n script.shcheck the syntax without running it
-
shellcheck script.shthe usual mistakes, with how to fix them (shellcheck)
Commands used
bashshellcheck
Each has a page in lore: lore bash opens it.
Related recipes
- ask for input in a scriptAsk yes or no, read a password without showing it, or wait for an answer with a time limit.
- check if a file or folder existsTest in a script whether a path exists, is a file, a folder, empty or runnable.
- check if a string contains a textTest in a script whether a string contains, starts with or matches something.
- find the script's own folderInside a script, find the folder the script is in, to reach files next to it.