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,

I guess the main answer to this would be:
since you wrote the script and your script controls the error checking and validations. I would say you would need to output the line number in your error statements. Even if the script would give a line number of a error if it is in a loop or a function call it may not be correct. So as in may other program languages you would need to put the reference to the error as needed. Now if using vi to edit the script to get a reference point in the vi session would be a

 <esc> :set nu 

But if the script changes so do the line numbers. So I think I would lean to a number system that is not reliant on the line number. Then you could search the code for the error number and find it where you expected the problem.

Hope this helps.

I'm not sure whether I conveyed what I wanted. For example in C programming, you can always print the line # using the macro __LINE__. Is there anything similar in bash that I could use of?

Thanks,

In bash:

#!/bin/bash

echo "Welcome to my script."
echo $LINENO
echo "The above variable should be: 4"

ok in a ksh shell the built in var is LINENO
so

 if [ chk -ne 0 ]; then #we are in error
     echo "It didn't make it at line: $LINENO"
fi

or something like that...

The $LINENO variable is the current line being executed so:

#!/bin/bash

err_out()
{
     echo "fatal error on line $1 status= $2 "
     exit 1
}
# example call of err_out
./mycommand
STATUS=$?
if [ $STATUS -ne 0 ] ; then
   l=$(( $LINENO - 3 ))
   err_out $l $STATUS
fi

Thanks a lot guys!!!