All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Allow == as synonym for = in test
@ 2011-03-06 23:01 David A. Wheeler
  2011-03-06 23:48 ` Jonathan Nieder
  0 siblings, 1 reply; 16+ messages in thread
From: David A. Wheeler @ 2011-03-06 23:01 UTC (permalink / raw)
  To: dash; +Cc: dwheeler

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},


^ permalink raw reply related	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2011-03-08 18:43 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-03-06 23:01 [PATCH] Allow == as synonym for = in test David A. Wheeler
2011-03-06 23:48 ` 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

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.