LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-04-2011, 02:48 PM   #1
suryaemlinux
Member
 
Registered: Mar 2009
Distribution: CentOS, Fedora
Posts: 34

Rep: Reputation: 0
Bash:Printing the line number in bash script


Hi,
I would like to know how do I print the line # in a script. My requirement is, I have a script which is about ~5000 lines long. If there are any errors happen I just exit. And I would like to add the line # of the script where the error happened.

Thanks,
 
Old 02-04-2011, 09:11 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
you can run your script using set -x or run the script with "bash -x script"
 
Old 02-05-2011, 09:59 AM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
BASH_LINENO, or LINENO.
Quote:
BASH_LINENO
An array variable whose members are the line numbers in source files corresponding to each member of FUNCNAME. ${BASH_LINENO[$i]} is the line number in the source file where ${FUNCNAME[$i]} was called (or ${BASH_LINENO[$i-1]} if referenced within another shell function). The corresponding source file name is ${BASH_SOURCE[$i]}. Use LINENO to obtain the current line number.
 
  


Reply



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
bash read a given line number from text bendeco13 Programming 7 08-31-2012 03:49 PM
Bash script question - formatting a number HarryBoy Linux - Newbie 10 08-31-2010 10:07 PM
bash : read every line from text file starting at given line number quadmore Programming 4 02-20-2009 12:29 PM
bash scripting : printing variable with the number within $i amdGTintel Programming 2 01-18-2007 01:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:09 AM.

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