Shell Commands
Command Execution
Coding Tips
Echo Function
Scripting Guide

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.

bash
1#!/usr/bin/env bash
2
3set -x
4echo "Downloading"
5cp source.txt target.txt
6set +x
7
8echo "Trace is off now"

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.

bash
1#!/usr/bin/env bash
2set -x
3
4echo "Start"
5pwd
6ls

You can also run an existing script this way:

bash
bash -x script.sh

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.

bash
1token="super-secret"
2set -x
3curl -H "Authorization: Bearer $token" https://example.com
4set +x

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.

bash
1#!/usr/bin/env bash
2
3export PS4='+ ${BASH_SOURCE}:${LINENO}: '
4set -x
5echo "Hello"
6set +x

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.sh would 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 -x to print shell commands as they are executed.
  • Use set +x to stop tracing again.
  • 'bash -x script.sh enables tracing without editing the script.'
  • Customize PS4 in Bash when you want more readable trace output.
  • Be careful not to expose secrets when tracing expanded commands.

Course illustration
Course illustration

All Rights Reserved.