Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 03-06-2011, 11:48 AM   #1
LQ Newbie
Registered: Apr 2009
Posts: 8

Rep: Reputation: 0
How to print lines when debugging shell script


I am testing some bash script using -fx flags:
#!/bin/bash -fx

Now when the program runs i get output in which i need to trace i my script the different lines.
Is there some built-in flag for line numbers output on debugging?

Old 03-06-2011, 12:07 PM   #2
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian except for netbook still on Slackware 13.37 (and I have to support Ubuntu)
Posts: 8,572
Blog Entries: 30

Rep: Reputation: 1182Reputation: 1182Reputation: 1182Reputation: 1182Reputation: 1182Reputation: 1182Reputation: 1182Reputation: 1182Reputation: 1182
Maybe (never tried it) export PS4='$LINENO: '

set -o functrace might help (also never tried it)
Old 03-06-2011, 03:27 PM   #3
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1955Reputation: 1955Reputation: 1955Reputation: 1955Reputation: 1955Reputation: 1955Reputation: 1955Reputation: 1955Reputation: 1955Reputation: 1955Reputation: 1955
The member GrapefruiTgirl has in the past posted a detailed PS4 line that gives you some useful information about your script:

A while back I modified it into a shell function to make it more convenient to use. I designed it to be added to your bashrc and exported so it becomes part of the shell environment.
#function for script debugging prompt
function s_dbg {

     export COLOR="\e[1;31m" CLEAR="\e[0m"
     help="$(  echo "Turn a detailed debugging prompt on or off.  Usage:"
               echo -e "${COLOR}\t$FUNCNAME on [verb] | $FUNCNAME off${CLEAR}"
               echo "Prints line# and last exit code value, as well as function name"
               echo "and relative subshell level if it goes above that of the script."
               echo "'verb' is an optional argument that turns on the shell's verbose behavior."
               echo "This function is designed to be loaded from bashrc and exported for use"
               echo "in scripts.  It doesn't do so well when called directly in an interactive shell."

     export bss=$((BASH_SUBSHELL+1)) #increment by one to counteract embedded test subshell below

     if [[ ! $* ]]; then
          echo "$help" && return
     elif [[ "$*" =~ verb ]]; then

     if [[ "$*" =~ on ]]; then
          export PS4="${COLOR}"'${LINENO}|ex:$?|${FUNCNAME[0]+Fn:${FUNCNAME[0]}|}$((($BASH_SUBSHELL > bss)) && echo "sbshl:$((BASH_SUBSHELL-1))|") '"$CLEAR"
          set -x $verb
          echo "${FUNCNAME} $@"
          echo "shell level:$SHLVL,subshell level:$BASH_SUBSHELL"
          unset PS4
          set +xv
} 2>/dev/null   #error redirect keeps function from outputting itself

export -f s_dbg   #export above function for use in scripts
I decided to remove $SHLVL from the prompt itself however, as I felt it has very limited informational value; the number only changes when a new bash instance is started after all.

Once exported, just add s_dbg on (verb) and s_dbg off to your script wherever you want the output to appear.
Old 03-07-2011, 03:54 AM   #4
LQ Newbie
Registered: Apr 2009
Posts: 8

Original Poster
Rep: Reputation: 0
Thx guys.
I will try this today.

Thx again.


bash, line, number, script, shell

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Seperating lines in a shell script slackwarefan Linux - Software 12 10-18-2009 03:10 PM
Align lines in shell script jeesun Linux - General 5 10-16-2009 12:29 AM
Shell script to jump over lines horacioemilio Programming 8 12-02-2007 05:44 PM
Shell script debugging rbautch Programming 11 07-20-2006 10:33 PM
Debugging a Bash Shell Script solarblast Linux - General 1 02-23-2006 01:44 AM

All times are GMT -5. The time now is 07:26 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration