[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: strange result loop exit status
From: |
Greg Wooledge |
Subject: |
Re: strange result loop exit status |
Date: |
Wed, 19 Mar 2014 10:27:03 -0400 |
User-agent: |
Mutt/1.4.2.3i |
On Wed, Mar 19, 2014 at 02:18:52PM +0400, Dmitry Arkhireev wrote:
> And if run it sample with echo $I after ((I++)) everything works as expected
> I=0; while [ $I -ne 1 ]; do ((I++)); echo $I; done; echo $?
> 1
> 0
Here, $? is the exit status of the echo, instead of the ((...)) command.
The ((...)) command returns a status of either 0 or 1, depending on the
value of the expression it evaluates.
With C-style post-increment (++) the expression evalutes to the original
value of the variable, rather than the incremented value.
imadev:~$ i=0; ((i++)); echo $?
1
imadev:~$ i=1; ((i++)); echo $?
0
imadev:~$ i=999; ((i++)); echo $?
0
When the original value is 0, the expression evaluates to "false"
(using the semantics of C), and "false" causes an exit status of 1
(using the semantics of Bash). When the original value is anything
other than 0, the expression evaluates to "true" (C-style), which
causes an exit status of 0 (Bash-style).