All of lore.kernel.org
 help / color / mirror / Atom feed
From: "David A. Wheeler" <dwheeler@dwheeler.com>
To: dash@vger.kernel.org
Cc: dwheeler@dwheeler.com
Subject: [PATCH] Allow == as synonym for = in test
Date: Sun, 06 Mar 2011 18:01:11 -0500 (EST)	[thread overview]
Message-ID: <E1PwMwp-0001iy-B5@fenris.runbox.com> (raw)

From: David A. Wheeler <dwheeler@dwheeler.com>

The following patch adds support for "==" as a synonym for "=" (is-string-equal) in test, including the documentation and comment changes to note it.

Why add this?
* Many current shell scripts use "==" instead of "=" to determine if strings are equal (as reported by https://wiki.ubuntu.com/DashAsBinSh and other places).  Bash, ksh, and probably other shells already support this, which is why its use is so common.
* Using "==" makes it slightly clearer that this is an equality test and not an assignment (since "=" is used elsewhere in shell for assignment).  Note that this is consistent with C, C++, and many other languages.
* A trivial amount of code is needed to implement it.

This isn't required by POSIX, but "test" in dash already supports tests not in POSIX.  I think it'd be useful to add one that's already in common use.

Signed-off-by: David A. Wheeler <dwheeler@dwheeler.com>

diff --git a/src/bltin/test.1 b/src/bltin/test.1
index 42435fb..56f8163 100644
--- a/src/bltin/test.1
+++ b/src/bltin/test.1
@@ -193,6 +193,12 @@ True if the strings
 and
 .Ar \&s\&2
 are identical.
+.It Ar \&s\&1 Cm \&=\&= Ar \&s\&2
+True if the strings
+.Ar \&s\&1
+and
+.Ar \&s\&2
+are identical (this is a synonym for \&=).
 .It Ar \&s\&1 Cm \&!= Ar \&s\&2
 True if the strings
 .Ar \&s\&1
diff --git a/src/bltin/test.c b/src/bltin/test.c
index 7888f38..5a581ed 100644
--- a/src/bltin/test.c
+++ b/src/bltin/test.c
@@ -31,7 +31,7 @@
        unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
                "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";

-       binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
+       binary-operator ::= "="|"=="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
                        "-nt"|"-ot"|"-ef";
        operand ::= <any legal UNIX file name>
 */
@@ -113,6 +113,7 @@ static struct t_op {
        {"-L",  FILSYM, UNOP},
        {"-S",  FILSOCK,UNOP},
        {"=",   STREQ,  BINOP},
+       {"==",  STREQ,  BINOP},
        {"!=",  STRNE,  BINOP},
        {"<",   STRLT,  BINOP},
        {">",   STRGT,  BINOP},


             reply	other threads:[~2011-03-06 23:24 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-06 23:01 David A. Wheeler [this message]
2011-03-06 23:48 ` [PATCH] Allow == as synonym for = in test Jonathan Nieder
2011-03-07  1:12   ` David A. Wheeler
2011-03-07 10:34     ` Guido Berhoerster
2011-03-07 14:55       ` David A. Wheeler
2011-03-07 17:18         ` Guido Berhoerster
2011-03-07 17:37           ` Dan Muresan
2011-03-07 22:00         ` Jonathan Nieder
2011-03-07 23:03           ` David A. Wheeler
2011-03-08  0:05             ` Guido Berhoerster
2011-03-08  0:03               ` Eric Blake
2011-03-08  7:17               ` Dan Muresan
2011-03-08 15:27                 ` Paul Smith
2011-03-08 15:53                   ` Dan Muresan
2011-03-08 18:13                 ` Guido Berhoerster
2011-03-08 18:43                   ` Dan Muresan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E1PwMwp-0001iy-B5@fenris.runbox.com \
    --to=dwheeler@dwheeler.com \
    --cc=dash@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.