help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Conditional expression evaluation using [ and [[ with -e


From: Pierre Gaston
Subject: Re: [Help-bash] Conditional expression evaluation using [ and [[ with -eq operator
Date: Fri, 15 Nov 2013 21:46:41 +0200

On Fri, Nov 15, 2013 at 7:41 PM, Peggy Russell
<address@hidden>wrote:

> I saw the snippet below the other day. Validating data I would use a
> regular
> expression or pattern in a 'if [[' or 'case', and not the below.
>
> But in this case, what is it about the command line processing steps for
> [ and [[, that the shell builtin test command, [, appears to have
> determined
> this was an integer expression, but the shell keyword compound command, [[,
> did not?
>
> I did refer to the man page, reference and
> http://mywiki.wooledge.org/BashFAQ/031.
>
> #!/bin/bash
> #set -vx
> declare var='a'
>
> if [ "$var" -eq "$var" ]; then
>   printf -- '[: true\n'
> else
>   printf --  '[: false\n'
> fi
>
> if [[ $var -eq $var ]]; then
>   printf --  '[[: true\n'
> else
>   printf --  '[[: false\n'
> fi
>
>
> Output:
> ./ztest: line 5: [: a: integer expression expected
> [: false
> [[: true
>
> Thank you.
> Peggy Russell
>
>
> [[ is evaluating the operands as  arithmetic expressions when you use -eq
-ne -lt etc...
so "a" is taken as the name of a variable and an empty variable evaluates
to 0 just like in the case of:
echo "$((emptyvar))"


reply via email to

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