[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Error message garbage when parameter expansion used inside (()) and vari
From: |
PRussell |
Subject: |
Error message garbage when parameter expansion used inside (()) and variable unset |
Date: |
Mon, 2 Apr 2018 16:16:54 -0500 |
Section 6.5 Shell Arithmetic says,
"Within an expression, shell variables may also be referenced by name without
using the parameter expansion syntax. A shell variable that is null or unset
evaluates to 0 when referenced by name without using the parameter expansion
syntax." - http://www.gnu.org/software/bash/manual/bash.html#Shell-Arithmetic
The above tells us what happens to an unset variable if not using parameter
expansion.
But if a shell variable uses parameter expansion and is null or unset, what
does it evaluate to inside (()) syntax?
It evaluates to 0 inside [[]], but gives an error inside (()). The error
message is different between bash 4.3.11 and 4.4.19. The later contains
garbage.
See test 3 and 4 below.
### Test 1:
# No parameter expansion. var2 not set. Should evaluate to 0 thus when compared
# to 0, 1A and 1B should echo yes. They do.
echo;echo 1A
( set -x;var=0;var1=var; [[ var1 -eq var2 ]] && echo yes || echo no )
echo 1B
( set -x;var=0;var1=var; (( var1 == var2 )) && echo yes || echo no )
### Test 2:
# No parameter expansion. var2 not set. Should evaluate to 0 thus when compared
# to 5, 2A and 2B should echo no. They do
echo;echo 2A
( set -x;var=5;var1=var; [[ var1 -eq var2 ]] && echo yes || echo no )
echo 2B
( set -x;var=5;var1=var; (( var1 == var2 )) && echo yes || echo no )
### Test 3:
# Parameter expansion. var2 not set.
echo;echo 3A
( set -x;var=0;var1=var; [[ var1 -eq $var2 ]] && echo yes || echo no )
echo 3B
( set -x;var=0;var1=var; (( var1 == $var2 )) && echo yes || echo no )
### Test 4:
echo;echo 4A
( set -x;var=5;var1=var; [[ var1 -eq $var2 ]] && echo yes || echo no )
echo 4B
( set -x;var=5;var1=var; (( var1 == $var2 )) && echo yes || echo no )
It appears that 3A and 4A evaluate to 0 because of the arithmetic context.
3A echo's yes; 4A echo's no.
The problem is what is happening with 3B and 4B. I tested on bash 4.3.11 and
bash 4.4.19 and got a slightly different error message.
Bash version 4.3.11:
./tt: line 18: var1: var1 == : syntax error: operand expected (error token is
"== ")
Bash version 4.4.19 (???? was garabage):
./tt: line 18: ????: var1 == : syntax error: operand expected (error token is
"== ")
Peggy Russell
- Error message garbage when parameter expansion used inside (()) and variable unset,
PRussell <=
- Re: Error message garbage when parameter expansion used inside (()) and variable unset, Greg Wooledge, 2018/04/03
- Re: Error message garbage when parameter expansion used inside (()) and variable unset, Daniel Mills, 2018/04/03
- Re: Error message garbage when parameter expansion used inside (()) and variable unset, Chet Ramey, 2018/04/03
- Re: Error message garbage when parameter expansion used inside (()) and variable unset, PRussell, 2018/04/03
- Re: Error message garbage when parameter expansion used inside (()) and variable unset, Greg Wooledge, 2018/04/03
- Re: Error message garbage when parameter expansion used inside (()) and variable unset, Chet Ramey, 2018/04/03
- Re: Error message garbage when parameter expansion used inside (()) and variable unset, PRussell, 2018/04/03
- Re: Error message garbage when parameter expansion used inside (()) and variable unset, Chet Ramey, 2018/04/04
- Re: Error message garbage when parameter expansion used inside (()) and variable unset, Chet Ramey, 2018/04/04