Unix Commands  «Prev  Next»
Lesson 3Tracing shell scripts
ObjectiveUse shell options to test each line of a script.

Tracing Shell Scripts

To locate problems in your script, the shell provides two options:
-v
and
–x .

The –x and –v options locate many, but not all, errors in a script

Which shell options should be used to test each line of a Shell script?

When it comes to writing shell scripts, testing each line is crucial to ensure that the script runs smoothly without any errors. To test each line of a shell script, there are several shell options that you can use. In this article, I will discuss the most commonly used shell options for testing each line of a shell script.
Shell Options for Testing Each Line of a Shell Script
  1. -n Option
  2. -v Option
  3. -x Option
  4. -e Option
  5. -u Option
  6. -f Option
  7. -s Option
  8. -t Option
  9. -z Option
  10. -a Option
  11. -r Option
  12. -w Option
  13. -x Option
  14. -o Option


Shell Options for Testing Each Line of a Shell Script

-n Option: The -n option is used to check if a variable or string is not empty. It returns true if the string is not empty and false if it is empty. Here's an example:
if [ -n "$var" ]; then
    echo "Variable is not empty"
fi

-v Option: The -v option is used to print each line of the script as it is executed. This is useful for debugging and troubleshooting purposes. Here's an example:
set -v
echo "This line will be printed"

-x Option: The -x option is similar to the -v option, but it also prints the value of variables as they are expanded. This is useful for debugging complex scripts. Here's an example:
set -x
var="Hello"
echo "The value of var is: $var"

-e Option: The -e option is used to exit the script if a command fails. This is useful to ensure that the script does not continue running if an error occurs. Here's an example:
set -e
ls /nonexistent_directory
echo "This line will not be executed"

-u Option: The -u option is used to exit the script if an undefined variable is used. This is useful to prevent errors caused by uninitialized variables. Here's an example:
set -u
echo "$undefined_variable"

-f Option: The -f option is used to check if a file exists and is a regular file. This is useful for checking if a file is present before performing operations on it. Here's an example:
if [ -f /path/to/file ]; then
    echo "File exists"
fi

-s Option: The -s option is used to check if a file exists and is not empty. This is useful for checking if a file contains any data before performing operations on it. Here's an example:
if [ -s /path/to/file ]; then
    echo "File is not empty"
fi

-t Option: The -t option is used to check if a file descriptor is associated with a terminal. This is useful for checking if a script is running in an interactive terminal or not. Here's an example:
if [ -t 0 ]; then
    echo "Script is running in an interactive terminal"
fi

-z Option: The -z option is used to check if a variable or string is empty. It returns true if the string is empty and false if it is not empty. Here's an example:
if [

Errors on the #! line of a script

Both options display the lines of your script before they are run, but use different techniques. Looking at the instructions as they are run is called tracing the script.

–v option

The –v option displays each line of your script before it is run. It prints the line exactly as you included it in your script
Running the script with the –v option shows you which option your script is running. The –v option helps when your script is producing no output and appears to be in an infinite loop. You must press CTRL + C to stop this sort of script. ]

–x option


The –x option displays each line with all variables translated into their values. This is helpful when your script uses variables. To debug a problem, you must know which values your script is using in its commands.
The code in the Slide Show below contains an error. The wrong variable name is used in the sort command. This produces no error message because it is not a syntax error . This is the kind of error that the –x option helps you locate. Look through the FlipBook to see how the –x option locates this error.

–x option locates this error.


  1. Here is the error3 script. The variable called column in the read statement is mistakenly called col in the sort command.
  2. We will sort the volleylist file, which uses a colon separator, on field 6. This information is given to the error3 script, which turns this info into a sort command.
  3. The output shows the file sorted on the first column (the player’s name) instead of column 6.
  4. The script is run again with the –x option. It’s waiting for the user to enter the file name, volleylist.
  5. The user types in volleylist, then 6 for the column, then : for the separator. You can see the tracing option print out each read command before the value is read from the screen.
  6. The tracing option displays the sort command before it is run. You can see the + on it’s own without the column argument. There should be a 6 here. This tells you there is something wrong with the $col variable in the sort command.
You may also use the –x or –v options for just a portion of your script. Here is more information about tracing a portion of a script

Tracing Script Portion

You may feel confident about part of your script but uncertain about another section. In this case, you would trace only the portion of your code that you suspect is causing a problem. You can turn on the tracing feature inside your script with the set command. Using set v inside your script prints each line before it is run. Use set +v to turn off this feature. You can do the same thing with set x and set +x to show lines of code with their variables translated.
In the following script set x and set v are combined into one statement to turn on both options in the middle of the script: #!

/bin/sh 
echo "Please enter name of new user: " 
read filename 

set xv sort $filename | cat n > tempfile cp tempfile $filename set +xv
echo "
Your file is now sorted"

Tracing Shell Scripts - Exercise


Click the Exercise link below to practice tracing a shell script. Tracing Shell Scripts - Exercise
The next lesson examines errors commonly seen when using the shell’s if statement.