How do you make `set -x` output more useful with `PS4`?
IntermediateConceptShellCheck, debugging, testing, portability and security
Answer
PS4 is the prefix printed before each traced command (default + ). It is expanded every time, so it can include the location:
export PS4='+ ${BASH_SOURCE##*/}:${LINENO}:${FUNCNAME[0]:-main}: '
bash -x ./deploy.sh
# + deploy.sh:42:rollout: kubectl rollout status deploy/api --timeout=120s
Use single quotes so the variables expand at trace time. Add $(date +%T) to spot slow steps, at the cost of a fork per command.
More on ShellCheck, debugging, testing, portability and security
What is ShellCheck and how do you use it in a team?BeginnerHow do you disable a ShellCheck warning, and when is that acceptable?IntermediateName the ShellCheck warnings you see most and what each means.IntermediateHow do you debug a Bash script?BeginnerWhat is bats, and how do you test a script that calls external commands?IntermediateWhat is `shfmt` and why use it alongside ShellCheck?Beginner