Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

How To Enable Shell Script Debugging Mode in Linux http://www.tecmint.

com/enable-shell-debug-mode-linux/

How To Enable Shell Script Debugging Mode


in Linux
by Aaron Kili | Published: November 28, 2016 | Last Updated: November 28, 2016

Download Your Free eBooks NOW - 10 Free Linux eBooks for Administrators | 4 Free Shell Scripting
eBooks SHARE

A script is simply a list of commands stored in a le. Instead of running a +

sequence of commands by typing them one by one all the time on the
terminal, a system user can store all of them (commands) in a le and

repeatedly invokes the le to re-execute the commands several times. 0

While learning scripting or during the early stages of writing scripts, we


normally start by writing small or short scripts with a few lines of 16
commands. And we usually debug such scripts by doing nothing more
than looking at their output and ensuring that they work as we intended.
31
However, as we begin to write very long and advanced scripts with
thousands of lines of commands, for instance scripts that modify system
settings, perform critical backups over networks and many more, we will
realize that only looking at the output of a script is not enough to nd bugs
within a script.

Therefore, in this shell script debugging in Linux series, we will walk


through how to enable shell script debugging, move over to explain the
different shell script debugging modes and how to use them in the
subsequent series.

How To Start A Script

1 di 7 28/11/2016 19:40
How To Enable Shell Script Debugging Mode in Linux http://www.tecmint.com/enable-shell-debug-mode-linux/

RedHat RHCSA and RHCE Certication Exam Study Ebook ($35) Get This Book

A script is distinguished from other les by its rst line, that contains a #!
(She-bang denes the le type) and a path name (path to interpreter)
which informs the system that the le is a collection of commands that
will be interpreted by the specied program (interpreter).

Below are examples of the rst lines in different types of scripts:

#!/bin/sh [For bash scripting]


#!/bin/bash [For shell scripting]
#!/usr/bin/perl [For perl programming]
#!/bin/awk -f [For awk scripting]

How To Execute A Shell Script in Linux


The conventional syntax for invoking a shell script is:

$ script_name argument1 ... argumentN

Another possible form is by clearly specifying the shell that will execute
the script as below:

$ shell script_name argument1 ... argumentN

For example:

$ /bin/bash script_name argument1 ... argumentN [For bash scripting]


$ /bin/ksh script_name argument1 ... argumentN [For ksh scripting]
$ /bin/sh script_name argument1 ... argumentN [For shell scripting]

Methods of Enabling Shell Script Debugging

2 di 7 28/11/2016 19:40
How To Enable Shell Script Debugging Mode in Linux http://www.tecmint.com/enable-shell-debug-mode-linux/

RedHat RHCSA and RHCE Certication Exam Study Ebook ($35) Get This Book

Below are the primary shell script debugging options:

-v (short for verbose) tells the shell to show all lines in a script
while they are read, it activates verbose mode.
-n (short for noexec or no ecxecution) instructs the shell read all
the commands, however doesnt execute them. This options activates
syntax checking mode.
-x (short for xtrace or execution trace) tells the shell to display all
commands and their arguments on the terminal while they are
executed. This option enables shell tracing mode.

1. Modifying the First Line of a Shell Script


The rst mechanism is by altering the rst line of a shell script as below,
this will enable debugging of the whole script.

#!/bin/sh option(s)

In the form above, option can be one or combination of the debugging


options above.

2. Invoking Shell With Debugging Options


The second is by invoking the shell with debugging options as follows, this
method will also turn on debugging of the entire script.

$ shell option(s) script_name argument1 ... argumentN

For example:

$ /bin/bash option(s) script_name argument1 ... argumentN

3. Using set Shell Built-in Command

3 di 7 28/11/2016 19:40
How To Enable Shell Script Debugging Mode in Linux http://www.tecmint.com/enable-shell-debug-mode-linux/

RedHat RHCSA and RHCE Certication Exam Study Ebook ($35) Get This Book
section of a shell script such as a function. This mechanism is important,
as it allows us to activate debugging at any segment of a shell script.

We can turn on debugging mode using set command in form below, where
option is any of the debugging options.

$ set option

To enable debugging mode, use:

$ set -option

To disable debugging mode, use:

$ set +option

In addition, if we have enabled several debugging modes in different


segments of a shell script, we can disable all of them at once like so:

$ set -

That is it for now with enabling shell script debugging mode. As we have
seen, we can either debug an entire shell script or a particular section of a
script.

In the next two episode of this series, we will cover how to use the shell
script debugging options to explain verbose, syntax checking and shell
tracing debugging modes with examples.

Importantly, do not forget to ask any questions about this guide or


perhaps provide us feedback through the comment section below. Until
then, stay connected to Tecmint.

4 di 7 28/11/2016 19:40
How To Enable Shell Script Debugging Mode in Linux http://www.tecmint.com/enable-shell-debug-mode-linux/

RedHat RHCSA and RHCE Certication Exam Study Ebook ($35) Get This Book

If You Appreciate What We Do Here On TecMint, You


Should Consider:
1. Stay Connected to: Twitter | Facebook | Google Plus

2. Subscribe to our email updates: Sign Up Now

3. Use our Linode referral link if you plan to buy VPS (it starts at only

$10/month).

4. Support us via PayPal donate - Make a Donation

5. Support us by purchasing our premium books in PDF format.

6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Tags: shell scripting

Aaron Kili View all Posts


Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin,

web developer, and currently a content creator for TecMint who loves working with
computers and strongly believes in sharing knowledge.

Your name can also be listed here. Got a tip? Submit it here to
become an TecMint author.

5 di 7 28/11/2016 19:40
How To Enable Shell Script Debugging Mode in Linux http://www.tecmint.com/enable-shell-debug-mode-linux/

RedHat RHCSA and RHCE Certication Exam Study Ebook ($35) Get This Book

PREVIOUS STORY NEXT STORY

Find Out All Live Hosts IP Addresses Cyber Monday SALE: Save an Extra 25% Off
Connected on Network in Linux On Our Security Deals For One Day Only

YOU MAY ALSO LIKE...

10 4 0

Working with Arrays in Linux Understanding and Writing An Insight of Linux


Shell Scripting Part 8 functions in Shell Scripts Variables in Shell Scripting
24 APR, 2014
Part VI Language Part 9
1 MAR, 2014 20 MAY, 2014

3 RESPONSES

Comments 3 Pingbacks 0

Leandro November 28, 2016 at 4:20 pm


The second snipped can be only used if execution ag is enabled. But you dont say anything related

6 di 7 28/11/2016 19:40
How To Enable Shell Script Debugging Mode in Linux http://www.tecmint.com/enable-shell-debug-mode-linux/

RedHat RHCSA and RHCE Certication Exam Study Ebook ($35) Get This Book
Reply

Leandro November 28, 2016 at 4:18 pm


Look the rst code snippet, there is something wrong there Sh for bash and bash for Shell?
Reply

Aaron Kili November 28, 2016 at 5:12 pm


@Leandro

It is a typo and thanks for catching that, we shall correct it and update the article.
Reply

GOT SOMETHING TO SAY? JOIN THE DISCUSSION.

Comment

Name * Email *

Website

Notify me of followup comments via e-mail. You can also subscribe without commenting.

Post Comment

7 di 7 28/11/2016 19:40

You might also like