Bash `set -x` command
BASH Programming
4 months ago
by Fahmida Yesmin
Bash set command is used for many purposes in bash and it has many options to enable or disable different types of features of the shell environment. The -x option is one of the options of the set command that is used to enable the debugging feature of the bash script for troubleshooting. The `set -x` command can be used from the terminal or inside any bash script. Different purposes for using the `set -x` command have been described in this tutorial.
Enable Debugging
If the script does not show the correct output or unexpected output then this problem can be solved by using debugging. The debugging feature can be enabled in different ways in the bash script which are mentioned below.
The debugging feature can be enabled by using the `set -x` command in the terminal or inside the bash script by using the `set –x` command.
The use of the `set -x` command has been shown in the next part of the tutorial.
The debugging feature can also be enabled by using the -x option at the time of executing the bash file like the following command:
$ bash -x filename.bash
The debugging feature can also be enabled by using the following line at the beginning of the script:
#!/bin/sh -x
Disable Debugging
It requires disabling the debugging feature of the bash script after solving the problem. The `set +x` command is used to disable the debugging feature.
Different Examples of the `set -x` Command
Two different uses of the `set -x` option have been shown by using examples.
Example-1: Enable and Disable the Debugging Feature
Create a bash file by using the following script that will take input from the user before and after using the `set -x` and `set +x` commands. The first name value will be taken and printed before using the `set -x` command.
The last name value will be taken and printed after using the `set -x` command. Next, the values of the first name and the last name will be printed after using the `set +x` command.
#!/bin/bash
#Take input and print
read -p “Enter your first name: ” firstname
echo “Your first name is $firstname”
#Enable the set -x command
set -x
#Take another input and print
read -p “Enter your last name: ” lastname
echo “Your last name is $lastname”
#Disable the set x command
set +x
#Print the variables
echo “Your fullname is $firstname $lastname”
Output:
The following output will be printed after executing the above script. According to the output, each statement of the script has been printed after executing the `set -x` command.
Example-2: Debugging Script Using `set -x` Command
Create a bash file with the following script that will take a file name from the user for reading by enabling the debugging feature.
The `set -x` command has been used at the beginning of the script to enable the debugging feature. Next, a filename will be taken from the user. If no file name is provided by the user then a message will be printed. If the filename taken from the user does not exist or is not a file then another error message will be displayed. If the valid filename is taken from the user then the content of the file will be printed.
#!/bin/bash
#Enable the debugging
set -x
#Take the filename from the user
echo -n “Enter a filename:”
read fn
#Check whether the filename is empty or not
if [ “$fn” == “” ];
then
echo “Filename is missing.”
#Check whether the file exists or not
elif [ ! -f “$fn” ];
then
#Print error message
echo “$fn does not exist.”
else
#Print the file content
cat “$fn”
fi
Output:
The following output will appear after executing the above script without providing any filename.
The output shows that the empty value has been taken as the filename. The `if` condition for checking an empty filename has been returned true for the empty input. So, the `echo` statement for printing error message has been printed for debugging option and the error message, “Filename is missing” has been printed.
The following output will appear after executing the above script with a filename. The output shows that the `if` condition for checking empty filename has been returned false and the `if` condition for checking non-existing file has been returned false also. The input filename, testfile.txt exists in the current location. So, the `cat` command for reading the testfile.txt file has been executed and the content of the file has been printed.
The following output will appear after executing the above script with a filename. The output shows that the `if` condition for checking an empty file name has been returned false and the `if` condition for checking a non-existing file has been returned true because the file1.
txt does not exist in the current location. So, the `cat` command for reading the file has not been executed and the error message, “file1.txt does not exist.” has been printed.
Conclusion
The debugging task is a very important part of any programming language to find out the error of the code easily. The debugging task is done in different ways for different programming languages. Multiple ways of enabling debugging feature for the bash script and the uses of the `set -x` command for debugging the bash script have been described in this tutorial.
About the author
Fahmida Yesmin
I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.
View all posts
bash -x command – Stack Overflow
Ask Question
Asked
Modified
3 years, 7 months ago
Viewed
78k times
I saw a client doing $bash -x command to see if the file is executable (or ksh -x command, etc.
) like the -x in the if statement in the shell script.
My question is: What does $bash -x command do?
My interpretation was to start a command in a new bash shell within the current shell, inheriting the same environment variables and executed by the same user.
The funny thing is I can do $ls but not $bash -x ls, which give:
(under AIX 6)
/usr/bin/ls: /usr/bin/ls: cannot execute binary file
It is a mystery for me why the error is – guessing it is due to a privilege which means my assumption above is not correct.
Also, I believe $bash ls and $bash -x ls is the same thing (-x for “execute”)?
Any comments are greatly appreciated.
Cheers!
6
The -x option starts a BASH shell in tracing mode. You can see all the details of how your command/script is processed.
It’s a good way to find some bugs if your script does not do what you would expect to
And, just as Alex said in a comment, to run a command in BASH, you have to use -c option like bash -x -c ls.
See man bash or the online manual, specifically the parts on invoking Bash and the set builtin command for more information:
All of the single-character options used with the
setbuiltin (see The Set Builtin) can be used as options when the shell is invoked.
-xPrint a trace of simple commands,
forcommands,casecommands,selectcommands, and arithmeticforcommands and their arguments or associated word lists after they are expanded and before they are executed. The value of thePS4variable is expanded and the resultant value is printed before the command and its expanded arguments.![]()
2
It appears that you have muddled these two.
Sign up or log in
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Post as a guest
Email
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.
Team X | Team X
Team X (eng.
Team X ) was a military unit for covert operations, created in the 1960s by the joint efforts of the subversive organization known as Weapons Plus and the Central Intelligence Agency of the US government under the control of the CIA operational major – Arthur Barrington.
Named Team X because it was made up of mutant agents, the group carried out several missions for the CIA, the Weapon Plus program and, though perhaps reluctantly, for the criminal mastermind known as Romulus, who used his personal agents undercover , Logan (codename: Wolverine) and Victor Creed (codename: Sabretooth) to pursue their interests. The team also included operatives David North and John Wraith, as well as the mysterious Mastodon and Wolverine’s former love, Silver Fox, although this was likely an impostor hired by the CIA, Weapon Plus, or Romulus himself.
Team X operatives also underwent multiple brainwashing sessions, with their memories largely altered or replaced with false, implanted Weapon Plus commands under the program’s chief scientist, Professor Thorton.
In 1963, agents Logan, codenamed Emilio Garra, and Sabretooth, codenamed El Tigre, were on a mission in Cuba after Kennedy’s assassination, where they were betrayed by the Silver Fox. They were later joined by agents Mastodon and Ghost, and together they encountered the Silver Fox known as Zora de Plata.
Agents Logan, Sabretooth and Wraith have been sent on a mission to Southeast Asia.
Also in 1963, scientists in Dallas, Texas experimented on Logan with carbonadium tablets. The entire squad was sent on Team X’s final mission to Berlin, Germany; they were ordered to rescue CIA double agent Janice Hollenbeck and steal a KGB carbonadium synthesizer needed by Soviet super-soldier Omega Red. In the following years, Omega Red constantly sought out the members of Team X, believing they had the information he needed.
In 1968, Wolverine was sent to kill Soviet super-soldier cosmonaut Red Epsilon to prevent the Russians from getting to the moon first. When Sabretooth fought his way through the KGB elite, border guards and internal security to deliver the order to cancel the mission from the very top of the food chain.
A few years after the Weapon Plus team disbanded, they formed their tenth iteration, the subversive Weapon X program, led by Professor Thornton and funded by the CIA through the USDA. Thornton successfully contacted or captured former Team X alumni who were infused with a serum that mimics Wolverine’s healing factor and given a new layer of memory implants, first through staged scripts and then through Aldo Ferro’s psychic powers.
Wolverine later successfully escaped from the Weapon X facility where he was being experimented on in Alberta, Canada while Mastodon retired, Ghost was hired by the US government, Sabretooth continued to work for Romulus, and the Silver Fox disappeared, and the only Maverick was still in the Weapon X program.
ZhFK team Project X, Women’s football in Russia. Official website
Team
Brief information about the command
ZHFC Project X
Calendar of past and future matches
Past matches
-
Futsal 2022/23
26 MAR.
/ Sun / 15:15 John Gault
1 ZhFC Project X
8 -
Futsal 2022/23
18 MAR. / Sat / 13:00
KSSHOR Zorkiy
4 ZhFC Project X
2 -
Futsal 2022/23
11 MAR.
/ Sat / 17:00 ZhFC Project X
6 John Gault
5 -
Futsal 2022/23
04 MAR. / Sat / 14:05
ZhFC Project X
2 ZhFK Youth
2 -
Futsal 2022/23
26 FEB.
/ Sun / 16:05 Victoria-Iksha
4 ZhFC Project X
0 -
Futsal 2022/23
19 FEB. / Sun / 16:00
ZhFK Project X
2 Legends Park
0 -
Futsal 2022/23
11 FEB.
/ Sat / 12:00 ZhFC Project X
1 KSSHOR Zorkiy
7 -
Futsal 2022/23
28 JAN. / Sat / 14:00
ZhFK Youth
0 ZhFK Project X
3 -
Futsal 2022/23
21 JAN.
/ Sat / 18:00 ZhFC Project X
3 Victoria-Iksha
3 -
Futsal 2022/23
15 JAN. / Sun / 16:00
Legends Park
0 ZhFC Project X
1 -
Futsal 2022/23
11 DEC.
2022 / 17:45 Rokada (Volgograd)
1 ZhFC Project X
5 -
Futsal 2022/23
03 DEC. 2022 / 14:00
Victoria-Iksha
1 ZhFK Project X
3
News
-
24.

/ Sun / 15:15
/ Sat / 17:00
/ Sun / 16:05
/ Sat / 12:00
/ Sat / 18:00 