__LINE__ equivalent for Linux shell
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,
开发者_JAVA百科Souvik
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.
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.
The $LINENO
variable returns the line in which that variable is used.
精彩评论