How to echo Shell Commands?

Linux provides the built-in “set” command line utility to print both the environment and shell variables. It supports a large variety of options to display the executed commands and their arguments in the output for debugging and troubleshooting purposes. The output displays the “enclosed” statement of the shell commands and not prints themselves. 

This guide explains the use of the “set” command to echo shell commands as they are executed or not:

  • Using “set -x” Command
  • Using “set -v” Command

Method 1: Using the “set -x” Command

The “x” option of the “set” command enables and disables the debugging feature in the bash script. It can be utilized within the bash script or in the terminal. It reads each line of the script and prints it alongside its argument in the output.

Let’s see its practical impanation using the bash script:

Create/Open a Script

A “script1.sh” bash script is taken as an example in the “nano” text editor:

$ nano script1.sh

Script

#!/bin/bash
set -x
a=10
echo "$a"
echo "This is the output"

Script Description:

  • The “#!/bin/bash denotes “Bash Shebang,” which will execute the “script1.sh” in the “bash” shell. 
  • The “setx” command will echo all shell commands of the script.
  • The “a” variable is declared as having an integer value of “10”.
  • The first “echo” command displays the “$a” variable value while the second “echo” will print its enclosed statement:

Execute the “script1.sh” script in the command line terminal:

$ ./script1.sh

The output shows “+” before each line and displays the shell commands in the terminal.

Disable the Printing of Shell Commands

To disable some commands, use the “set +x” option before them. In the above modified “script1.sh”, use the “set +x” to stop echo the last “echo” command:

Script

#!/bin/bash
set -x                                            #Turn On
a=10
echo "$a"
set +x                                             #Turn Off
echo "This is the output"

Save and exit the script.

Run the modified “script1.sh”:

$ ./script1.sh

Currently, the last echo command does not print itself, only its output is displayed.

Method 2: Using the “set -v” Command

The “v” is another useful option of the “set” utility to print the input shell commands as they are executed/read. It does not print any special character or symbol before each line of the script as the “set -x” command. 

Let’s see how it works in a bash script to echo the shell commands:

Create/Open the Bash Script

The “program.sh” is an existing bash script taken as an example in the “nano” text editor:

$ nano program.sh

Script:

#!/bin/bash
set -v
function hello()
{
echo "Hello Linux!"
}

echo "The hello function is correct"

Script Description:

  • set -v: Prints each line of the script alongside the shell commands.
  • hello:  Denotes a function with no argument having an “echo” command.
  • echo: Last command in the script to print the enclosed statements.

Run the “program.sh” script to check the output:

$ ./program.sh

The output displays each shell command defined in the script. 

Disable the Printing of Shell Commands

The “v” option of the “set” command disables the printing of the shell command. It can be used at any place in the shell script.

For the practical implementation, we placed it before the last “echo” statement in the modified “program.sh” bash script: 

Script:

#!/bin/bash
set -v                                                           #Turn On
function hello()
{
echo "Hello Linux!"
}
set +v                                                          #Turn Off
echo "The hello function is correct"

Run the “program.sh” script:

$ ./program.sh

The “set +v” itself printed because of the “set -v” command and disabled the printing of the last echo command.

Conclusion

Linux offers “set -x” and “set -v” commands to echo the defined set of shell commands. The alternatives of both these commands are “set +x” and “set +v” which disables the printing feature of shell commands. All these commands can be implemented at any place in the shell script.

This guide has provided all possible methods to echo shell commands as they are executed.