views:

47

answers:

3

Hello,

I am writing a considerably huge shell script. Is there any way to print the line number of the script from that script? Basically I want to have something similar to gcc LINE macro. This will help me to debug my script.

Thanks in advance,

Souvik

+2  A: 

Maybe this helps a little bit:

http://www.network-theory.co.uk/docs/bashref/BashVariables.html

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. The corresponding source file name is ${BASH_SOURCE[$i]}. Use LINENO to obtain the current line number. 
Tomasz Kowalczyk
A: 

The $LINENO variable returns the line in which that variable is used.

Delan Azabani
A: 

Had to do it myself a while ago and found this useful article back then: http://aymanh.com/how-debug-bash-scripts#adding-line-numbers-to-tracing-output

Essentially you need to use $LINENO to get the current line in the script.

halfdan
Thanks everyone.. $LINENO contains the line number value.
Souvik