Re: if [[ $Something ]] – in what situations does this work?

Johnny Rosenberg gurus.knugum at gmail.com
Sat Feb 18 15:31:29 UTC 2012


2012/2/18 Robert P. J. Day <rpjday at crashcourse.ca>:
> On Sat, 18 Feb 2012, franz.reitinger wrote:
>
>> First of all I suppose you're using the bash.
>> Amoung others the shell treats several parameters specially; these
>> parameters may only be referenced and assignement to them is not
>> allowed.
>>
>> One of them is $?, which expands to the exit status of the most
>> recently executed command. However, the exit status 0 means that the
>> command has been executed without errors; any other return value has to
>> be interpreted as error code.
>> e.g.
>> # any command ...
>> if [ $? -ne 0 ]; then
>>     # error handling
>> fi
>
>  i'd have to check but i think the constuct
>
>  if [[ $VAR ]]
>
> does nothing but check if either that variable is defined, or if it
> has a non-zero string length.  so no matter what the value of $? is,
>
>  if [[ $? ]]
>
> will always evaluate to true.  you should verify that yourself,
> though.
>
>  if you're checking the returned value of $?, you should always
> compare it explicitly.

Yes, you're right!

~$ xyz=""
~$ if [[ $xyz ]]; then echo "True"; else echo "False"; fi
False
~$ xyz="a"
~$ if [[ $xyz ]]; then echo "True"; else echo "False"; fi
True
~$

Thanks!


Kind regards

Johnny Rosenberg
ジョニー・ローゼンバーグ!

>
> rday
>
> --




More information about the ubuntu-users mailing list