Trace the commands actually executed after variable expansion and branching.
Show each command immediately before execution.
Which command is appropriate?
bash -x enables xtrace and writes expanded simple commands to standard error before execution. Avoid exposing secrets in traced values.
Detailed explanation
bash -n script.shIncorrect. bash -n checks syntax without normal execution.
bash -v script.shIncorrect. bash -v shows input lines before expansion.
bash -e script.shIncorrect. bash -e changes exit behavior after failures; it is not the tracing option.
bash -x script.shCorrect. bash -x traces expanded commands just before execution.
Try it yourself
An example you can run in a temporary verification environment.
bash -c 'f=$(mktemp); printf "name=blue\nprintf \"%%s\\n\" \"$name\"\n" >"$f"; bash -x "$f" 2>&1; rm -f "$f"'Expected result
+ name=blue、+ printf ... blueの追跡とblueの実行結果Key points
- -x enables execution tracing
- It shows expanded commands
- Do not expose secrets
Notes
- Environment: GNU Bash 5.2 / 一時ファイル
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Shell function
A reusable group of commands that runs in the caller's shell context.
Positional parameter
A numbered argument or special value supplied to a script or function.
Control flow
Conditions, loops, and exit statuses determine which shell commands run.