Behaviour of an empty conditional expression is undefined?
Here shows the output of conditional expressions: infinite:~# [[ ]] -bash: syntax error near unexpected token `]]' infinite:~# [[ ]] infinite:~# [[ "$A" ]] -bash: syntax error near unexpected token `"$A"' infinite:~# [[ "$A" ]] infinite:~# [[ "$A" ]] infinite:~# [[ ]] infinite:~# [[ "$A" ]] -bash: syntax error near unexpected token `"$A"' infinite:~# echo $? 2 infinite:~# [[ "$A" ]] infinite:~# echo $? 1 infinite:~# infinite:~# bash --version bash --version GNU bash, version 5.2.15(1)-release (x86_64-pc-linux-gnu) The variable $A has never been defined. Double brackets surrounding 1) a few spaces or 2) an empty variable even quoted behave illogically. I've also try different lengths of spaces, and also an empty variable without quotes, the same illogical. I am curious if I am the first guy encounter such a situation. My observation: 1) An empty conditional expression will first triger an syntax error, return with code 2; 2) Immediate another empty conditional expression won't triger such an error, return with code 1; 3) Subsequent a conditional expression with empty variables will triger the error again; 4) The same conditional expression won't triger the error until different commands are input; I guess it has something to do with the cache of command evaluation. It seems there is a counter recording some state. I hope my guess may help in debugging. According to the manual, I think an empty condition like ``[[ ]]'' and ``[[ $EMPTY ]]'' should always triger syntax error, but a condition with an empty string like ``[[ "" ]]'' and ``[[ "$EMPTY" ]]'' should evaluate to false or return 1. Best regards, Alex
Behaviour of an empty conditional expression is undefined?
Here shows the behavious of empty conditional expressions: infinite:~# [[ ]] -bash: syntax error near unexpected token `]]' infinite:~# [[ ]] infinite:~# [[ "$A" ]] -bash: syntax error near unexpected token `"$A"' infinite:~# [[ "$A" ]] infinite:~# [[ "$A" ]] infinite:~# [[ ]] infinite:~# [[ "$A" ]] -bash: syntax error near unexpected token `"$A"' infinite:~# echo $? 2 infinite:~# [[ "$A" ]] infinite:~# echo $? 1 infinite:~# infinite:~# bash --version bash --version GNU bash, version 5.2.15(1)-release (x86_64-pc-linux-gnu) The variable $A has never been defined. Double brackets surrounding 1) a few spaces or 2) an empty variable even quoted behave illogically. I've also try different lengths of spaces, and also an empty variable without quotes, the same illogical. I am curious if I am the first guy encounter such a situation. My observation: 1) An empty conditional expression will first triger an syntax error, return with code 2; 2) Immediate another empty conditional expression won't triger such an error, return with code 1; 3) Subsequent a conditional expression with empty variables will triger the error again; 4) The same conditional expression won't triger the error until different commands are input; I guess it has something to do with the cache of command evaluation. It seems there is a counter recording some state. I hope my guess may help in debugging. According to the manual, I think an empty condition like ``[[ ]]'' and ``[[ $EMPTY ]]'' should always triger syntax error, but a condition with an empty string like ``[[ "" ]]'' and ``[[ "$EMPTY" ]]'' should evaluate to false or return 1.
Re: Behaviour of an empty conditional expression is undefined?
i [[ daily , never had such.. sounds like u dont type in C in utf8 On Thu, Feb 23, 2023, 9:58 AM Qingsheng Huang wrote: > Here shows the behavious of empty conditional expressions: > > > infinite:~# [[ ]] > -bash: syntax error near unexpected token `]]' > infinite:~# [[ ]] > infinite:~# [[ "$A" ]] > -bash: syntax error near unexpected token `"$A"' > infinite:~# [[ "$A" ]] > infinite:~# [[ "$A" ]] > infinite:~# [[ ]] > infinite:~# [[ "$A" ]] > -bash: syntax error near unexpected token `"$A"' > infinite:~# echo $? > 2 > infinite:~# [[ "$A" ]] > infinite:~# echo $? > 1 > infinite:~# > infinite:~# bash --version > bash --version > GNU bash, version 5.2.15(1)-release (x86_64-pc-linux-gnu) > > > The variable $A has never been defined. > Double brackets surrounding 1) a few spaces or 2) an empty variable even > quoted behave illogically. I've also try different lengths of spaces, and > also an empty variable without quotes, the same illogical. > > I am curious if I am the first guy encounter such a situation. > My observation: > 1) An empty conditional expression will first triger an syntax error, > return >with code 2; > 2) Immediate another empty conditional expression won't triger such an > error, >return with code 1; > 3) Subsequent a conditional expression with empty variables will triger the >error again; > 4) The same conditional expression won't triger the error until different >commands are input; > > I guess it has something to do with the cache of command evaluation. > It seems there is a counter recording some state. I hope my guess may help > in debugging. > > According to the manual, I think an empty condition like ``[[ ]]'' and > ``[[ $EMPTY ]]'' should always triger syntax error, but a condition with > an empty string like ``[[ "" ]]'' and ``[[ "$EMPTY" ]]'' should evaluate > to false or return 1. >
Re: Behaviour of an empty conditional expression is undefined?
On Thu, 23 Feb 2023 16:26:43 +0800 (CST) "Qingsheng Huang" wrote: > Here shows the behavious of empty conditional expressions: > > > infinite:~# [[ ]] > -bash: syntax error near unexpected token `]]' > infinite:~# [[ ]] > infinite:~# [[ "$A" ]] > -bash: syntax error near unexpected token `"$A"' > infinite:~# [[ "$A" ]] > infinite:~# [[ "$A" ]] > infinite:~# [[ ]] > infinite:~# [[ "$A" ]] > -bash: syntax error near unexpected token `"$A"' > infinite:~# echo $? > 2 > infinite:~# [[ "$A" ]] > infinite:~# echo $? > 1 > infinite:~# > infinite:~# bash --version > bash --version > GNU bash, version 5.2.15(1)-release (x86_64-pc-linux-gnu) > > > The variable $A has never been defined. > Double brackets surrounding 1) a few spaces or 2) an empty variable even > quoted behave illogically. I've also try different lengths of spaces, and > also an empty variable without quotes, the same illogical. > > I am curious if I am the first guy encounter such a situation. Well observed. I can confirm this strange behaviour. I just want to add that 5.1.16 behaves sensibly, with [[ ]] always raising an error in an interactive shell. -- Kerin Millar
Re: Behaviour of an empty conditional expression is undefined?
On Thu, 23 Feb 2023 11:07:52 +0100 alex xmb ratchev wrote: > i [[ daily , never had such.. This observation isn't helpful unless you account for which version of bash you are using. In any case, the report is accurate and it describes a post-5.1 regression. -- Kerin Millar
Re: Behaviour of an empty conditional expression is undefined?
On Thu, Feb 23, 2023, 11:14 AM Kerin Millar wrote: > On Thu, 23 Feb 2023 11:07:52 +0100 > alex xmb ratchev wrote: > > > i [[ daily , never had such.. > > This observation isn't helpful unless you account for which version of > bash you are using. In any case, the report is accurate and it describes a > post-5.1 regression. > i see , thank you sir , .. i rather newest .. -- > Kerin Millar >
Re: Behaviour of an empty conditional expression is undefined?
2023年2月23日(木) 17:58 Qingsheng Huang : > The variable $A has never been defined. > Double brackets surrounding 1) a few spaces or 2) an empty variable even > quoted behave illogically. I've also try different lengths of spaces, and > also an empty variable without quotes, the same illogical. > > I am curious if I am the first guy encounter such a situation. This is already reported [1] and fixed in devel [2]. [1] https://lists.gnu.org/archive/html/bug-bash/2022-10/msg00098.html [2] https://git.savannah.gnu.org/cgit/bash.git/commit/?h=devel&id=6711d2dc7793ed3d35e91b2bd4ecbdfccfcf469f
Re: Behaviour of an empty conditional expression is undefined?
On 2/23/23 3:26 AM, Qingsheng Huang wrote: Here shows the behavious of empty conditional expressions: infinite:~# [[ ]] -bash: syntax error near unexpected token `]]' infinite:~# [[ ]] infinite:~# [[ "$A" ]] -bash: syntax error near unexpected token `"$A"' This is a bug having to do with resetting the parser state in an interactive shell after an error token. It was fixed back in October as the result of https://savannah.gnu.org/support/?110745 and the fix is in the devel branch. It's ironic that this is the exact same issue we discussed yesterday in https://lists.gnu.org/archive/html/bug-bash/2023-02/msg00150.html with a slightly different effect. According to the manual, I think an empty condition like ``[[ ]]'' and ``[[ $EMPTY ]]'' should always triger syntax error, No. There has to be at least one operand of [[, but since word expansions are not performed until [[ executes, a variable expansion is a valid operand. That's the difference between a compound command and a builtin. -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/