zsh-users
 help / color / mirror / code / Atom feed
* string equal problem
@ 2011-03-07  9:04 Lyre
  2011-03-07  9:13 ` Mikael Magnusson
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Lyre @ 2011-03-07  9:04 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 357 bytes --]

To my surprise,the following statment:

if [ "abc" == "def" ]; then echo y; else echo n; fi

doesn't work, it says "zsh: = not found".

Then I tried it in several version of zsh (4.3.x) on different distribution.
All of them doesn't work, except the zsh 4.2.0 on sles9.

What's the problem with it, is there something wrong in my configuration?

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

* Re: string equal problem
  2011-03-07  9:04 string equal problem Lyre
@ 2011-03-07  9:13 ` Mikael Magnusson
  2011-03-07  9:13 ` Frank Terbeck
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: Mikael Magnusson @ 2011-03-07  9:13 UTC (permalink / raw)
  To: Lyre; +Cc: zsh-users

On 7 March 2011 10:04, Lyre <4179e1@gmail.com> wrote:
> To my surprise,the following statment:
>
> if [ "abc" == "def" ]; then echo y; else echo n; fi
>
> doesn't work, it says "zsh: = not found".

you want either [ "abc" = "def" ] or [[ "abc" == "def" ]] (= will work
in [[]] too).

-- 
Mikael Magnusson


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

* Re: string equal problem
  2011-03-07  9:04 string equal problem Lyre
  2011-03-07  9:13 ` Mikael Magnusson
@ 2011-03-07  9:13 ` Frank Terbeck
  2011-03-07  9:27   ` Frank Terbeck
  2011-03-07  9:15 ` Wendell Hom
  2011-03-07  9:26 ` Bart Schaefer
  3 siblings, 1 reply; 7+ messages in thread
From: Frank Terbeck @ 2011-03-07  9:13 UTC (permalink / raw)
  To: Lyre; +Cc: zsh-users

Lyre wrote:
> To my surprise,the following statment:
>
> if [ "abc" == "def" ]; then echo y; else echo n; fi
>
> doesn't work, it says "zsh: = not found".

`==' only works in "[[ ... ]]". So, either use

     if [ "abc" = "def" ]; then
or
     if [[ "abc" == "def" ]]; then

Regards, Frank

-- 
In protocol design, perfection has been reached not when there is
nothing left to add, but when there is nothing left to take away.
                                                  -- RFC 1925


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

* Re: string equal problem
  2011-03-07  9:04 string equal problem Lyre
  2011-03-07  9:13 ` Mikael Magnusson
  2011-03-07  9:13 ` Frank Terbeck
@ 2011-03-07  9:15 ` Wendell Hom
  2011-03-07  9:26 ` Bart Schaefer
  3 siblings, 0 replies; 7+ messages in thread
From: Wendell Hom @ 2011-03-07  9:15 UTC (permalink / raw)
  To: Lyre, zsh-users

[-- Attachment #1: Type: text/plain, Size: 640 bytes --]

Your syntax is off, it should be

if [[ "abc" == "def" ]]; then echo y; else echo n; fi

-Wendell





________________________________
From: Lyre <4179e1@gmail.com>
To: zsh-users@zsh.org
Sent: Mon, March 7, 2011 1:04:48 AM
Subject: string equal problem

To my surprise,the following statment:

if [ "abc" == "def" ]; then echo y; else echo n; fi

doesn't work, it says "zsh: = not found".

Then I tried it in several version of zsh (4.3.x) on different distribution.
All of them doesn't work, except the zsh 4.2.0 on sles9.

What's the problem with it, is there something wrong in my configuration?



      

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

* Re: string equal problem
  2011-03-07  9:04 string equal problem Lyre
                   ` (2 preceding siblings ...)
  2011-03-07  9:15 ` Wendell Hom
@ 2011-03-07  9:26 ` Bart Schaefer
  3 siblings, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 2011-03-07  9:26 UTC (permalink / raw)
  To: Lyre, zsh-users

On Mar 7,  5:04pm, Lyre wrote:
} Subject: string equal problem
}
} if [ "abc" == "def" ]; then echo y; else echo n; fi
} 
} doesn't work, it says "zsh: = not found".

 14.7.3 `=' expansion
 --------------------
 If a word begins with an unquoted `=' and the EQUALS option is set, the
 remainder of the word is taken as the name of a command.  If a command
 exists by that name, the word is replaced by the full pathname of the
 command.

What the doc doesn't go on to say is that if a command does NOT exist
by that name, it's an error.

The "test" command/builtin, for which "[" is an alias, doesn't normally
allow "==" as an operator; rather, it uses "=" for this comparison.

    /usr/bin/test: ==: binary operator expected

As it happens, zsh does allow == as an operator for test, but you must
either quote it or unsetopt EQUALS, because the arguments of test are
subject to filename expansion.

If you want to use == without messing with the option, try this way:

    if [[ "abc" == "def" ]]; then echo y; else echo n; fi

The "[[" reserved word imposes different parsing rules on the expression
it introduces, so there == is not subject to expansion.

} All of them doesn't work, except the zsh 4.2.0 on sles9.

SUSE must unsetopt EQUALS in /etc/zshenv, or some other startup file,
because no zsh since around version 2 (maybe longer) has been different
in this regard.


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

* Re: string equal problem
  2011-03-07  9:13 ` Frank Terbeck
@ 2011-03-07  9:27   ` Frank Terbeck
  2011-03-07  9:58     ` Micah Elliott
  0 siblings, 1 reply; 7+ messages in thread
From: Frank Terbeck @ 2011-03-07  9:27 UTC (permalink / raw)
  To: Lyre; +Cc: zsh-users

Frank Terbeck wrote:
> Lyre wrote:
>> To my surprise,the following statment:
>>
>> if [ "abc" == "def" ]; then echo y; else echo n; fi
>>
>> doesn't work, it says "zsh: = not found".
>
> `==' only works in "[[ ... ]]". [...]

Actually, that's not quite true.

`[' works like a command, thus == is seen like it is for any other
command too. And in those cases, the leading equal leads to expansion of
the corresponding program's full path. For example

zsh% print =emacs
/usr/bin/emacs

Now, there is no command "=" on your system, which is why you're getting
the correct message "= not found". To prevent this, you can either unset
the `EQUALS' option (man zshoptions | less -p EQUALS) or quote the
string that is otherwise subject to this expansion. As in:

       if [ 'abc' '==' 'def' ]; then

Regards, Frank

-- 
In protocol design, perfection has been reached not when there is
nothing left to add, but when there is nothing left to take away.
                                                  -- RFC 1925


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

* Re: string equal problem
  2011-03-07  9:27   ` Frank Terbeck
@ 2011-03-07  9:58     ` Micah Elliott
  0 siblings, 0 replies; 7+ messages in thread
From: Micah Elliott @ 2011-03-07  9:58 UTC (permalink / raw)
  To: Frank Terbeck, zsh-users; +Cc: Lyre

Just to clarify, it appears that the form used in the OP is frowned
upon. From "run-help test" (equivalent to [ ]):

  ...Users are urged wherever possible  to  use the `[[' test syntax
which does not have these ambiguities.

-- 
twitter:@MicahElliott  |  email:mde@MicahElliott.com  |  http://MicahElliott.com


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

end of thread, other threads:[~2011-03-07  9:59 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-03-07  9:04 string equal problem Lyre
2011-03-07  9:13 ` Mikael Magnusson
2011-03-07  9:13 ` Frank Terbeck
2011-03-07  9:27   ` Frank Terbeck
2011-03-07  9:58     ` Micah Elliott
2011-03-07  9:15 ` Wendell Hom
2011-03-07  9:26 ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).