Created
May 10, 2019 17:24
-
-
Save pawamoy/cca35f0f5ccd56665d6421e9b2d2d4bc to your computer and use it in GitHub Desktop.
LINENO in Bash and ZSH
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
echo $( | |
# comment | |
echo "hello" \ | |
"world" \ | |
"thanks" | # comment | |
cat | ( # comment | |
# comment | |
cat -n # comment | |
) | cat && # comment | |
echo bye | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PS4='+$BASH_SOURCE:$LINENO> '
Note how the line numbers in Zsh's output are correct, while Bash's line numbers are completely off. In fact, they are not really off, they are offset:
echo
command starts at line 1 but finishes at line 11. It explains the last line of the debug output.echo hello world thanks
echo
was concatenated, and the offset was not retained, so it continues at 14 forcat
, when it should have continued at 16 (in Zsh,echo
was at line 3, nextcat
was at line 6, not line 4). By continuing at 14, it makes calculating the real position using the offset impossible. If it continued at 16, we could have got the correct line number by doing some magic like 16 - 11 + 1 = 6. For this you'd need to know there's an offset of course, which is not trivial either... Here we're left with a 14 that means nothing unless you parse/lex the entire script to understand what's going on.cat -n
at line 16cat
andecho bye
, lines 17 and 18So, these line numbers make sense, but as a shell script writer, when I use a variable that tells me the line number, I expect it to show me the number of the actual line, not an offset increased by one each time a command is ran or a comment is met. It makes debugging even harder, and using debug output impossible in some cases (think coverage).
This, here, could be the main, and single, actual reason for me to finally switch over to Zsh :p