Debian Bug report logs - #850202
dash: test with multiple conditions cannot handle variable whose expanded value is same as an operator

version graph

Package: dash; Maintainer for dash is Andrej Shadura <andrewsh@debian.org>; Source for dash is src:dash (PTS, buildd, popcon).

Reported by: J G Miller <miller@yoyo.ORG>

Date: Wed, 4 Jan 2017 23:57:02 UTC

Severity: normal

Found in version dash/0.5.7-4

Reply or subscribe to this bug.

Toggle useless messages

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to debian-bugs-dist@lists.debian.org, Gerrit Pape <pape@smarden.org>:
Bug#850202; Package dash. (Wed, 04 Jan 2017 23:57:04 GMT) (full text, mbox, link).


Acknowledgement sent to J G Miller <miller@yoyo.ORG>:
New Bug report received and forwarded. Copy sent to Gerrit Pape <pape@smarden.org>. (Wed, 04 Jan 2017 23:57:04 GMT) (full text, mbox, link).


Message #5 received at submit@bugs.debian.org (full text, mbox, reply):

From: J G Miller <miller@yoyo.ORG>
To: Debian Bug Tracking System <submit@bugs.debian.org>
Subject: dash: test with multiple conditions cannot handle variable whose expanded value is same as an operator
Date: Thu, 05 Jan 2017 00:53:57 +0100
Package: dash
Version: 0.5.7-4+b1
Severity: normal

In Bourne shell script with dash, using either [ ] or test,

   test -n "${variable}" && echo "variable is set to -->${variable}<--"

works as expected even if the value of variable is to a value the same
as a test operator eg -ne or -nt

   variable="-ne" ; test -n "${variable}" && echo "variable is set to -->${variable}<--"
   variable is set to -->-ne<--

However the addition of another test condition with -a or -o results in an
unexpected behavior in that the expanded variable is now treated as an operator
and not just as the string as should be the case.

    variable="-ne" ; test -n "${variable}" -a -n "${DISPLAY}" && echo "variable is set to -->${variable}<--"
    dash: 1: test: Illegal number: -n

Applying quoted parentheses makes no difference.

    variable="-ne" ; test \( -n "${variable}" \) -a \( -n "${DISPLAY}" \) && echo "variable is set to -->${variable}<--"
dash: 2: test: Illegal number: -n

Similarly bad behavior is seen with a variable set to "-nt", which is how I stumbled upon this problem.

    variable="-nt" ; test \( -n "${variable}" \) -a \( -n "${DISPLAY}" \) && echo "variable is set to -->${variable}<--"
dash: 3: test: closing paren expected

So if the test is consists of a single condtion, the expanded variable does not cause
problems with the test, and should not affect the test if it is part of a multiple condition test.

Incidentally bash also has this problem.

So the short term kludge in a shell script is to do

     if [ -n "${variable_with_problem_value}" ]
     then
          if [ \( other_condition1 \) -a \( other_condition2 \) ]
          then
               ...



-- System Information:
Debian Release: 8.6
  APT prefers stable-updates
  APT policy: (500, 'stable-updates'), (500, 'stable')
Architecture: armel (armv5tel)

Kernel: Linux 3.16.0-4-kirkwood
Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=ANSI_X3.4-1968) (ignored: LC_ALL set to C)
Shell: /bin/sh linked to /bin/dash
Init: systemd (via /run/systemd/system)

Versions of packages dash depends on:
ii  debianutils  4.4+b1
ii  dpkg         1.17.27
ii  libc6        2.19-18+deb8u6

dash recommends no packages.

dash suggests no packages.

-- debconf information:
* dash/sh: true



Send a report that this bug log contains spam.


Debian bug tracking system administrator <owner@bugs.debian.org>. Last modified: Tue Sep 20 22:39:13 2022; Machine Name: buxtehude

Debian Bug tracking system

Debbugs is free software and licensed under the terms of the GNU Public License version 2. The current version can be obtained from https://bugs.debian.org/debbugs-source/.

Copyright © 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson, 2005-2017 Don Armstrong, and many other contributors.