Bash -x and Set -x

Difference between bash -x and set -x

The bash -x and set -x are both used to enable debugging in a Bash script. The primary difference between the two is when they are used.

The bash -xcommand

bash -x is used when running a script — it turns on debugging for the entire script. For example, if you have a script called my_script.sh, you would run it with bash -x my_script.sh to enable debugging for the entire script. Here are a few examples of how bash -x can be used in a Bash script:

  1. Debugging an entire script:

#!/bin/bash
name="John Doe"
echo "Hello, $name"

To run this script with debugging in the below terminal, we would run it with bash -x script.sh.

Terminal 1
Terminal
Loading...

When this script runs, it will display the following output:

+ name='John Doe'
+ echo 'Hello, John Doe'
Hello, John Doe
  1. Debugging an entire script with a function:

#!/bin/bash
my_function() {
  echo "Inside my_function"
}
my_function

To run this script with debugging in the terminal below, we would run it with bash -x script.sh.

Terminal 1
Terminal
Loading...

When this script runs, it will display the following output:

+ my_function
+ echo 'Inside my_function'
Inside my_function
  1. Debugging an entire script with a loop:

#!/bin/bash
for i in {1..5}; do
    echo $i
done

To run this script with debugging in the terminal, we would run it with bash -x script.sh.

Terminal 1
Terminal
Loading...

When this script runs, it will display the following output:

+ echo 1
1
+ for i in {1..5}
+ echo 2
2
+ for i in {1..5}
+ echo 3
3
+ for i in {1..5}
+ echo 4
4
+ for i in {1..5}
+ echo 5
5

The set -xcommand

set -x is used within a script – it turns on debugging for the current shell session. For example, if you have a script called my_script.sh that contains the following line:

set -x

When the script runs, debugging will be enabled for the current shell session, and any commands executed after the set -x line will be displayed with their expanded values. Examples of set -x in bash are below:

  1. Debugging a variable assignment:

#!/bin/bash
set -x
name="John Doe"
echo "Hello, $name"
set +x

We run the above code in the terminal below by creating an executable my_script.sh file.

Terminal 1
Terminal
Loading...

When this script runs, it will display the following output:

+ name='John Doe'
+ echo 'Hello, John Doe'
Hello, John Doe
  1. Debugging a function:

#!/bin/bash
set -x

my_function() {
  echo "Inside my_function"
}
my_function
set +x

We run the above code in the terminal below by creating an executable my_script.sh file.

Terminal 1
Terminal
Loading...

When this script runs, it will display the following output:

+ my_function
+ echo 'Inside my_function'
Inside my_function
  1. Debugging a loop:

#!/bin/bash
set -x
for i in {1..5}; do
    echo $i
done
set +x

We run the above code in the terminal below by creating an executable my_script.sh file.

Terminal 1
Terminal
Loading...

When this script runs, it will display the following output:

+ for i in {1..5}
+ echo 1
1
+ for i in {1..5}
+ echo 2
2
+ for i in {1..5}
+ echo 3
3
+ for i in {1..5}
+ echo 4
4
+ for i in {1..5}
+ echo 5
5

In summary, bash -x is used to enable debugging for an entire script when it's run, whereas set -x is used to enable debugging for the current shell session within a script.

Free Resources