0

Using a subshell environment in an if statement screws up my $LINENO variable. Here is a minimal reproducible code:

1. #!/bin/bash
2. if true ; then
3.     (echo dummy)
4. fi
5. echo "Line no:" $LINENO

It shows "Line no: 4" instead of 5. Is that a normal behavior? I could not find any explanations online. The problem occurs only if the condition is true (meaning the subshell is executed). Same problem occurs if the subshell is in a while/for loop iterating at least once.

Surprisingly the three pieces of code below work as expected:

a) no if-then-fi

1. #!/bin/bash
2. (echo dummy)
3. echo "Line no:" $LINENO

shows 3 as expected.

b) no subshell

1. #!/bin/bash
2. if true ; then
3.     echo dummy
4. fi
5. echo "Line no:" $LINENO

shows 5 as expected.

c) nested subshells

1. #!/bin/bash
2. (if true ; then
3.     (echo dummy)
4. fi)
5. echo "Line no:" $LINENO

shows 5 as expected.

5
  • I can't reproduce the problem. I tried both 4.2.3 on Linux and 3.2.57 on Mac.
    – Barmar
    Jul 5, 2019 at 22:11
  • Nor at ideone.com: ideone.com/oHtUvU
    – Barmar
    Jul 5, 2019 at 22:13
  • I am using bash 5.0.3 on Linux 5.0.0 (default on Ubuntu 19.04)
    – Charles
    Jul 5, 2019 at 22:17
  • 1
    Sounds like a bug you should report to the bash developers, it's not a problem with your script.
    – Barmar
    Jul 5, 2019 at 22:25
  • Thanks. I've reported it.
    – Charles
    Jul 5, 2019 at 22:31

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.