I understand I could use $LINENO
while debugging bash script to print out line number.
#!/bin/bash
set -x
PS4='${LINENO}: '
echo "hello"
set +x
Prints out the following if I run the script
$ ./script.sh
+ PS4='${LINENO}: '
4: echo hello
hello
That looks great. I can clearly see that the echo
came from line 4.
But, if I source the script, the line number gets somehow duplicated.
$ source script.sh
33: PS4='${LINENO}: '
44: echo hello
hello
55: set +x
Not sure what I’m doing wrong, but obviously my script doesn’t have line numbers up to 33, 44, 55. It just seems like the line numbers are wrong. Why would sourcing the sourcing the script give this strange output?