Bash error re $ LINENO-- or am I just confused?

Consider:

  #! / bin / bash

 echo '
 '$ LINENO
 echo '' '
 '$ LINENO

The first echo prints 4 correctly, but the second echo prints 5 instead of 6. Am I missing something or is this an error? (Using bash 3.00.15)

+6
source share
2 answers

Looks like a failed implementation (bug) in bash.

I used:

#!/bin/bash -p echo $LINENO echo ' ' $LINENO ' ' $LINENO ' ' $LINENO echo '' ' ' $LINENO 

which gave:

 2 3 3 3 6 

Which supports the theory that a variable is evaluated before the shell believes that the string has been completed. Once the line is completed, he will update LINENO and continue.

Bash verified versions: 3.2.48 (mac), 4.1.5 (linux)

When I use the syntax:

 echo ' ' $LINENO 

he gets a new line number. This seems to be due to the evaluation of the empty string being carried as the only argument in the string.

+4
source

Bash seems to interpret the multi-line and multi-line argument for the echo command for one line of the source file (script), because Bash must concatenate the multi-string and multi-line argument into the echo command in one (one) argument. The concatenation mechanism is also triggered by an empty string '' , followed by a string containing the newline character echo -e '' + '\n' + $LINENO .

 #!/bin/bash # Bash concatenates a multi-string & multi-line argument ... echo ' ' $LINENO ' ' $LINENO ' ' $LINENO # ... into a one line argument. echo -e "' ' $LINENO '\n' $LINENO '\n' $LINENO\n" 

 #!/bin/bash echo "2 3 4 5 6 LINENO: $LINENO" # 6 LINENO: 6 exit 

 #!/bin/bash echo "2" " " " 3 4 5 6 LINENO: $LINENO" # 6 LINENO: 2 # echo -e "2" + " " + "\n3\n4\n5\n6 LINENO: $LINENO" exit 
+1
source

Source: https://habr.com/ru/post/890618/


All Articles