bug-bash
[Top][All Lists]
Advanced

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

Re: The usage of [[ (not with if)


From: Bob Proulx
Subject: Re: The usage of [[ (not with if)
Date: Wed, 4 Aug 2010 09:11:24 -0600
User-agent: Mutt/1.5.18 (2008-05-17)

Peng Yu wrote:
> I have the following script and output. The man page says "Return  a
> status  of  0 or 1 depending on the evaluation of the conditional
> expression expression." Therefore, I thought that the two printf
> statements should print 1 and 0 respectively. But both of them print
> 0. I'm wondering what [[ should return if it is not used with a if
> statement.

The problem is that you have confused the character output to stdout
with the exit code status return of the program.

> $ cat main.sh
> #!/usr/bin/env bash
> 
> printf '%d\n' `[[ 10 -gt 1 ]]`
> printf '%d\n' `[[ 1 -gt 10 ]]`

The backticks invoke the command, save the character output to stdout,
replace the backticks with that output.  The exit code is not used in
your example.  The output of those is nothing.  When printf evaluates
nothing as an integer it resolves to 0 and therefore 0 is output.

Look at this:

  $ [[ 10 -gt 1 ]]
  $ [[ 1 -gt 10 ]]

Neither of those produce any output.

  $ printf '%d\n' ""
  0

To print the exit code you would need something like this:

  $ [[ 10 -gt 1 ]] ; echo $?
  0
  $ [[ 1 -gt 10 ]] ; echo $?
  1

Bob



reply via email to

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