[PATCH] Allow == as synonym for = in test

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: David A. Wheeler <dwheeler@xxxxxxxxxxxx>

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@xxxxxxxxxxxx>

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

--
To unsubscribe from this list: send the line "unsubscribe dash" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [LARTC]     [Bugtraq]     [Yosemite Forum]     [Photo]

  Powered by Linux