bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Behaviour of an empty conditional expression is undefined?


From: Qingsheng Huang
Subject: Behaviour of an empty conditional expression is undefined?
Date: Thu, 23 Feb 2023 16:13:11 +0800 (CST)

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

reply via email to

[Prev in Thread] Current Thread [Next in Thread]