Why is LINENO inconsistent after multiline piped commands?

Viewed 57

Line numbers generated when executing the following script with bash (5.1.0) are unexpected:

trap 'echo $LINENO' DEBUG

:
(:) | 
# Add any number of lines here
:
: 

This returns 3 - 6 - 5, while I expected to get 3 - 6 - 7.

The issue appears only when the pipe contains at least one sub-shell (created with ( )) before the last command.

The last command may span any number of lines, they are not taken into account.

The following variations of the script behave as expected:

  • No sub-shell (3 - 4 - 6 - 7):

    trap 'echo $LINENO' DEBUG
    
    :
    : | 
    # Add any number of lines here
    :
    :
    
  • last piped command stays on the same line (3 - 4 - 5):

    trap 'echo $LINENO' DEBUG
    
    :
    (:) | :
    :
    
  • sub-shell only on the last piped command (3 - 4 - 8):

    trap 'echo $LINENO' DEBUG
    
    :
    : | (
    :
    )
    :
    
1 Answers

Following the interesting comment from @Barmar, I tested with several versions of bash. By bisecting I could find out the change of behavior appeared between 4.4.23 and 5.0.0:

$ docker run -it -v "$(pwd)/test.sh":/tmp/test.sh bash:4.4.23 bash /tmp/test.sh
3
6
7
$ docker run -it -v "$(pwd)/test.sh":/tmp/test.sh bash:5.0.0 bash /tmp/test.sh
3
6
5

This has been reported as an issue in the bash bug list: https://savannah.gnu.org/support/?110714

Related