Operating System - HP-UX
1827603 Members
3410 Online
109966 Solutions
New Discussion

How to get line number inside unix shell script

 
SOLVED
Go to solution
Vimala L
Occasional Advisor

How to get line number inside unix shell script

Hello there

Is there any way to print a script's line numbers for every echo in that shell script?

For eg.
-------------
#!/bin/sh
X="abc"
echo $X Lineno is $line
echo date is `date` $line
Y="bcd:"
echo $Y line no is $line
-------------
I expect the script to print
abc Lineno is 3
date is Sat Sep 14 13:28:52 EAT 2002 4
bcd: line no is 6
-------------------

Looking for something equivalent to __LINE__ in 'C' programs.

This would be quite useful for debugging.

Thanks,
Vimala.
3 REPLIES 3
Denver Osborn
Honored Contributor
Solution

Re: How to get line number inside unix shell script

For the posix shell (/bin/sh) have it echo the LINENO variable.

#!/bin/sh
X="abc"
echo $X Lineno is $LINENO
echo date is `date` $LINENO
Y="bcd:"
echo $Y line no is $LINENO

Hope this helps,
-denver
Denver Osborn
Honored Contributor

Re: How to get line number inside unix shell script

To have it echo back the actual line number...

X="abc"
echo $X Lineno is $((LINENO -1))
harry d brown jr
Honored Contributor

Re: How to get line number inside unix shell script


OF course you COULD run your script with the -x option to display the commands...


live free or die
harry
Live Free or Die