LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Bash:Printing the line number in bash script (http://www.linuxquestions.org/questions/programming-9/bash-printing-the-line-number-in-bash-script-860759/)

suryaemlinux 02-04-2011 02:48 PM

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,

ghostdog74 02-04-2011 09:11 PM

you can run your script using set -x or run the script with "bash -x script"

ntubski 02-05-2011 09:59 AM

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.



All times are GMT -5. The time now is 10:51 AM.