On 7/23/23 9:20 AM, Rob Landley wrote:

$ bash -c $'echo $LINENO\necho $(echo $LINENO\necho $LINENO\n); echo $LINENO'
0
3 4
3
$ bash -c $'echo $LINENO\necho $LINENO $(echo $LINENO\necho $LINENO\n); echo 
$LINENO'
0
1 1 2
3

Why does the 3 4 turn into 1 2? (Where does it get "3" from the
first time?)

This is how a bison parser works. It can't tell that a WORD starts a simple
command until it parses one more token (it could be a function definition,
for example). When it builds a simple command, it uses the current line
number as the line number that starts the simple command.

In the first case, the second word is not complete until line 3, when the
command substitution completes, so that is the line number of the simple
command. The command substitution, when it executes, inherits the line
number of the simple command, and adds 1 because of the embedded newline.

In the second case, the second word is $LINENO, so the current line number
is 1 (2 in current versions, since -c command initializes to 1). Everything
else works as before.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    c...@case.edu    http://tiswww.cwru.edu/~chet/

_______________________________________________
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to