Path: csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail From: Greg Wooledge Newsgroups: gnu.bash.bug Subject: Re: numerical comparison missing in bash and expr Date: Wed, 11 Mar 2020 09:24:24 -0400 Lines: 46 Approved: bug-bash@gnu.org Message-ID: References: <20200311132424.GD845@eeg.ccf.org> NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: usenet.stanford.edu 1583933070 27499 209.51.188.17 (11 Mar 2020 13:24:30 GMT) X-Complaints-To: action@cs.stanford.edu Cc: bug-bash@gnu.org, bash@packages.debian.org To: "Joseph A. Russo" Envelope-to: bug-bash@gnu.org Mail-Followup-To: "Joseph A. Russo" , bug-bash@gnu.org, bash@packages.debian.org Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 139.137.100.1 X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <20200311132424.GD845@eeg.ccf.org> X-Mailman-Original-References: Xref: csiph.com gnu.bash.bug:16020 On Wed, Mar 11, 2020 at 07:29:38AM -0400, Joseph A. Russo wrote: > # the following two lines work > [ 5 < 10 ] && echo true || echo false > [ 5 > 10 ] && echo true || echo false You've misunderstood the syntax here. The < sign introduces a redirection of standard input, from a file named "10". What you've written is equivalent to [ 5 ] < 10 && echo true || echo false The command [ 5 ] tests whether the string "5" has a non-zero length. It does, and so the command returns "true" (exit status 0). The SECOND command uses > which introduces an output redirection, to a file named "10". I believe you will find there is an empty file named "10" in your working directory now, assuming you didn't delete it after your tests. > # the next three lines fail > n=5 > [ $n < 10 ] && echo true || echo false > [ $n > 10 ] && echo true || echo false These commands are identical to the previous set. The only difference is whether the file named "10" existed or not. If these "failed", it's perhaps because you ran these commands first, before the file existed. And you ran the first set of commands second, after the file was created, which suppresses the error you would have got from the input redirection when the file was missing. There are various correct ways to perform integer comparisons in bash. From oldest to newest: test "$n" -lt 10 [ "$n" -lt 10 ] [[ $n -lt 10 ]] ((n < 10)) The first two are POSIX compatible. The second two are bash extensions.