[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bash doesn't print an error message for [[ $str =~ $invalid_re ]]
From: |
Emanuele Torre |
Subject: |
bash doesn't print an error message for [[ $str =~ $invalid_re ]] |
Date: |
Wed, 15 Jun 2022 19:23:30 +0200 |
Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -march=x86-64 -mtune=generic -O2 -pipe -fno-plt
-DDEFAULT_PATH_VALUE='/usr/local/sbin:/usr/local/bin:/usr/bin'
-DSTANDARD_UTILS_PATH='/usr/bin' -DSYS_BASHRC='/etc/bash.bashrc'
-DSYS_BASH_LOGOUT='/etc/bash.bash_logout'
-DNON_INTERACTIVE_LOGIN_SHELLS
uname output: Linux t420 5.15.46-1-lts #1 SMP Thu, 09 Jun 2022
10:12:44 +0000 x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu
Bash Version: 5.1
Patch Level: 16
Release Status: release
Description:
bash doesn't print an error message when an invalid regex is
used with the [[ $str =~ $re ]] operator.
It simply makes [[ $str =~ $invalid_re ]], return 2 (and not set
BASH_REMATCH)
bash-5.1$ [[ abc =~ .* ]];printf %s\\n "$?";declare -p BASH_REMATCH
0
declare -a BASH_REMATCH=([0]="abc")
bash-5.1$ re='('
bash-5.1$ [[ xyz =~ $re ]];printf %s\\n "$?";declare -p BASH_REMATCH
2
declare -a BASH_REMATCH=([0]="abc")
bash-5.1$ [[ xx =~ yy ]];printf %s\\n "$?";declare -p BASH_REMATCH
1
declare -a BASH_REMATCH=()
GNU grep and GNU sed print an error message for example:
bash-5.1$ sed '/[c-_]/d'
sed: -e expression #1, char 7: Invalid range end
bash-5.1$ grep -E '(' <<<''
grep: Unmatched ( or \(
bash could at least print a message that says that the regex could not
be compiled. (bash can probably already tell that error occurs because
it returns 2.)
Additional Information:
An error message is especially imporant since bash's [[ behaves
weirdly when an error occurs for one of the operators:
[[ $str =~ $invalid_re || abc ]]
bash, instead of making the whole [[ command return non-zero, runs it
as it would run:
[[ $str =~ $invalid_re ]] || [[ abc ]]
so there is no way to tell if an error occured in a `[[' command that
uses =~ in combination with other operators (even if you check $?).
For arithmetic operators, bash also behaves as described above.
bash-5.1$ [[ ++ -gt 3 ]]; printf %s\\n "$?"
bash: ++: syntax error: operand expected (error token is "+")
1
bash-5.1$ [[ ++ -gt 3 || abc ]]; printf %s\\n "$?"
bash: ++: syntax error: operand expected (error token is "+")
0
But at at least, it prints an error message so you can tell an error
occured.
(also note that here `[[ ++ -gt 3 ]]' returns 1 and not 2 even if an
error occured and was reported by printing an error message, this also
happens for `(( ++ ))' and `let ++' and has happened since bash2
afaict, so probably not worth changing, I am just pointing it out.)
I think this behaviour: [[ ERROREXPR || OTHEREXPR ]] running OTHEREXPR
even if ERROREXPR cause an error weird, because:
1) if one expression has an error, i think the whole command should be
affected by the error.
2) ksh's [[ doesn't beheve like that.
$ [[ ++ -gt 3 ]]
ksh: ++: more tokens expected
$ print -r -- "$?"
1
$ [[ ++ -gt 3 || abc ]]
ksh: ++: more tokens expected
$ print -r -- "$?"
1
3) bash's own [ doesn't behave like that.
bash-5.1$ [ abc -le 3 ]
bash: [: abc: integer expression expected
bash-5.1$ printf '%s\n' "$?"
2
bash-5.1$ [ abc -le 3 -o abc ]
bash: [: abc: integer expression expected
bash-5.1$ printf '%s\n' "$?"
2
bash has also had this [[ since bash2, so i don't know if it's worth
changing it, this report is mostly about adding an error message for
[[ $str =~ $invalid_re ]].
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- bash doesn't print an error message for [[ $str =~ $invalid_re ]],
Emanuele Torre <=