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.