How to echo shell commands as they are executed
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If you want a shell script to print each command as it runs, the usual tool is shell tracing. In POSIX-style shells such as Bash and sh, that generally means enabling set -x, which prints commands after expansion and before execution.
Use set -x and set +x
The most common approach is to turn tracing on for the part of the script you want to inspect and then turn it off again.
This is ideal when you only need visibility around one block of logic.
Enable Tracing for the Whole Script
If you want every command traced, you can enable it near the top of the script or start the shell with -x.
You can also run an existing script this way:
That is useful when you do not want to edit the script just to debug it.
Understand What Gets Printed
Tracing shows commands after variable expansion, which is helpful for debugging but can also expose secrets.
That trace will print the expanded token value. Be careful when tracing scripts that touch credentials, API keys, or personal data.
Customize the Trace Prefix in Bash
In Bash, the PS4 variable controls the prefix for traced lines. This can make traces easier to read.
Adding the file name and line number is especially helpful in larger scripts.
Use the Right Tool for the Environment
The general idea exists outside Bash too, but the exact mechanism differs by environment. In Makefiles, shell tracing belongs to the shell running the recipe. In PowerShell, tracing uses PowerShell-specific tools. If the question is specifically about a Unix shell script, set -x is the right default answer.
That keeps the advice simple and aligned with what most shell users actually need.
Turn Tracing On Only Around Problem Areas
For large scripts, tracing every command can produce too much noise. A narrower traced region often makes debugging faster because the output stays focused on the section that is actually failing.
Redirect Trace Output When Needed
In Bash, trace output normally goes to standard error. That is useful because you can capture or redirect it separately from normal command output when debugging CI jobs or noisy deployment scripts.
That separation is often the difference between readable debug logs and a wall of mixed output.
It is useful in CI.
Common Pitfalls
- Forgetting to disable tracing and accidentally dumping secrets into logs.
- Confusing command tracing with command output redirection or general logging.
- Editing a script permanently when
bash -x script.shwould have been enough. - Assuming every shell or automation environment uses the exact same tracing syntax.
- Leaving noisy tracing enabled in production scripts after debugging is complete.
Summary
- Use
set -xto print shell commands as they are executed. - Use
set +xto stop tracing again. - '
bash -x script.shenables tracing without editing the script.' - Customize
PS4in Bash when you want more readable trace output. - Be careful not to expose secrets when tracing expanded commands.

