help-bash
[Top][All Lists]
Advanced

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

[Help-bash] Conditional expression evaluation using [ and [[ with -eq op


From: Peggy Russell
Subject: [Help-bash] Conditional expression evaluation using [ and [[ with -eq operator
Date: Fri, 15 Nov 2013 17:41:47 +0000 (UTC)
User-agent: Loom/3.14 (http://gmane.org/)

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




reply via email to

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