[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Bug in Bash on syntax error in functions?
From: |
Roel Van de Paar |
Subject: |
Bug in Bash on syntax error in functions? |
Date: |
Fri, 8 Apr 2022 16:42:38 +1000 |
Hi!
I am using GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu).
Here is what looks to be a bug:
------------------------------------------------------------------------------------------
$ cat testfail1
#!/bin/bash
echo 'a'
fail_command
echo 'b'
$ cat testfail2
#!/bin/bash
echo 'a'
echo "$[ 1 + ]"
echo 'b'
$ cat testfail3
#!/bin/bash
function1(){
echo 'a'
fail_command
echo 'b'
}
function1
echo "exit: $?"
$ cat testfail4
#!/bin/bash
function1(){
echo 'a'
echo "$[ 1 + ]"
echo 'b'
}
function1
echo "exit: $?"
$ ./testfail1
a
./testfail1: line 3: fail_command: command not found
b
$ ./testfail2
a
./testfail2: line 3: 1 + : syntax error: operand expected (error token is
"+ ")
b
$ ./testfail3
a
./testfail3: line 4: fail_command: command not found
b
exit: 0
$ ./testfail4
a
./testfail4: line 4: 1 + : syntax error: operand expected (error token is
"+ ")
exit: 1
$
------------------------------------------------------------------------------------------
Here is what is inconsistent:
1) testfail4 performs differently from testfail3 in that the function
immediately terminates when a syntax error is found, even though both
scripts have a failing command on the same line. Note testfail3 continues
to execute whereas testfail4 does not, even in the case of nested functions
[1]
2) testfail4 performs differently from testfail2 in that when the syntax
error is found a function, that function terminates, whereas if the same
syntax error is in the main part of the script, that script continues to
execute
[1] Nested functions example: same outcome; all functions are immediately
terminated
------------------------------------------------------------------------------------------
$ cat testfail5
#!/bin/bash
function2(){
function1
echo 'c'
}
function1(){
echo 'a'
echo "$[ 1 + ]"
echo 'b'
}
function2
echo "exit: $?"
$ ./testfail5
a
./testfail5: line 8: 1 + : syntax error: operand expected (error token is
"+ ")
exit: 1
------------------------------------------------------------------------------------------
Please confirm that this is a bug and if another ticket is needed. Thank you
Bug fix proposal would be to treat functions in the same way as the main
script; always just keep executing, no matter what error (syntax or command
not found or any other).
This was worth a few hours of debugging in a large script running in a test
server that oddly 'skipped code' when there was a calculation error, with
the skipped code creating some undefined behaviour.
Thank you
- Bug in Bash on syntax error in functions?,
Roel Van de Paar <=