Path: csiph.com!goblin2!goblin1!goblin.stu.neva.ru!usenet.stanford.edu!not-for-mail From: Eric Blake Newsgroups: gnu.bash.bug Subject: Re: built-in '[' and '/usr/bin/[' yield different results Date: Tue, 13 Nov 2018 11:54:29 -0600 Organization: Red Hat, Inc. Lines: 52 Approved: bug-bash@gnu.org Message-ID: References: <5b86cf1c-d39a-befc-9734-5be8abd5cddd@heinzerling.com> NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Trace: usenet.stanford.edu 1542131701 11873 208.118.235.17 (13 Nov 2018 17:55:01 GMT) X-Complaints-To: action@cs.stanford.edu To: Service , bug-bash@gnu.org, bash@packages.debian.org Envelope-to: bug-bash@gnu.org User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.0 In-Reply-To: <5b86cf1c-d39a-befc-9734-5be8abd5cddd@heinzerling.com> Content-Language: en-US X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.45]); Tue, 13 Nov 2018 17:54:31 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com gnu.bash.bug:14814 On 11/13/18 10:29 AM, Service wrote: > =C2=A0=C2=A0 =C2=A0# ensure that file1 exists and that file2 does not = exist There's your problem. It is inherently ambiguous what timestamp to use=20 when a file is missing (infinitely new or infinitely old, or always an=20 error for not existing); bash's -nt picked one way, while other shells=20 have picked the other. POSIX is silent on the matter (-nt is an=20 extension outside of POSIX), so there is nothing portable you can rely on= . > =C2=A0=C2=A0 =C2=A0/bin/touch file1 > =C2=A0=C2=A0 =C2=A0/bin/rm -f file2 > =C2=A0=C2=A0 =C2=A0# built-in > =C2=A0=C2=A0 =C2=A0if=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0 [ file1 -nt file2 ]; then echo nt; else echo not_nt; fi > =C2=A0=C2=A0 =C2=A0# external > =C2=A0=C2=A0 =C2=A0if /usr/bin/[ file1 -nt file2 ]; then echo nt; else= echo not_nt; fi >=20 > =C2=A0=C2=A0 =C2=A0# Output is as expected: > =C2=A0=C2=A0 =C2=A0nt > =C2=A0=C2=A0 =C2=A0nt That is, bash's builtin '[' and coreutil's external '[' happened to pick=20 the same thing: a missing file is treated as infinitely old. >=20 > =C2=A0=C2=A0 =C2=A02. This does not work: >=20 > =C2=A0=C2=A0 =C2=A0# Put the above commands into a script, say check.s= h > =C2=A0=C2=A0 =C2=A0# Run with: /bin/sh < check.sh > =C2=A0=C2=A0 =C2=A0# Or=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 : /bin/sh ./chec= k.sh > =C2=A0=C2=A0 =C2=A0# Or=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 : /usr/bin/env .= /check.sh >=20 > =C2=A0=C2=A0 =C2=A0# Output is always not ok: > =C2=A0=C2=A0 =C2=A0not_nt > =C2=A0=C2=A0 =C2=A0nt Most likely, this is because your /bin/sh is not bash, but probably=20 dash, and dash has picked a missing file as being treated as always an=20 error. That does not make it a bug in bash, though, but a difference in=20 behavior of your /bin/sh. --=20 Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org