Sample Header Ad - 728x90

PS4: debug bash script with $LINENO: execute vs source

2 votes
2 answers
2013 views
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?
Asked by user3240688 (121 rep)
Feb 25, 2021, 04:29 PM
Last activity: Sep 11, 2024, 09:51 AM