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

J dreadpiratejeff at gmail.com
Sat Feb 18 15:26:26 UTC 2012


On Sat, Feb 18, 2012 at 10:02, Robert P. J. Day <rpjday at crashcourse.ca> wrote:
> 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,

Correct:

bladernr at klaatu:~$ A=foo
bladernr at klaatu:~$ if [[ $A ]]; then echo YES; fi
YES
bladernr at klaatu:~$ if [[ $B ]]; then echo YES; fi

AFAIK, that syntax will only work on user defined variables...



>
>  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.
>
> rday
>
> --
>
> ========================================================================
> Robert P. J. Day                                 Ottawa, Ontario, CANADA
>                        http://crashcourse.ca
>
> Twitter:                                       http://twitter.com/rpjday
> LinkedIn:                               http://ca.linkedin.com/in/rpjday
> ========================================================================
>
> --
> ubuntu-users mailing list
> ubuntu-users at lists.ubuntu.com
> Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users




More information about the ubuntu-users mailing list