zsh-users
 help / color / mirror / code / Atom feed
* Use of == in functions
@ 2020-01-12  8:34 Frank Gallacher
  2020-01-12 10:09 ` Kusalananda Kähäri
       [not found] ` <20200112100906.GA95942__47727.4053309642$1578823915$gmane$org@pooh.prefix.duckdns.org>
  0 siblings, 2 replies; 5+ messages in thread
From: Frank Gallacher @ 2020-01-12  8:34 UTC (permalink / raw)
  To: zsh-users

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

I am converting my functions from bash to zsh, when I do something like:


ciao.old () {
	if [ $# == 0 ]
	then
		PARM="-h" 
	else
		PARM=$* 
	fi


I get an error like:

% ciao.old          
ciao.old:8: = not found


Using the old FORTRAN syntax, Replacing == with -eq fixes the problem; is this a bug, or just being tricky???

TIA, Frankie G.

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

* Re: Use of == in functions
  2020-01-12  8:34 Use of == in functions Frank Gallacher
@ 2020-01-12 10:09 ` Kusalananda Kähäri
  2020-01-12 10:33   ` SUMMARY " Frank Gallacher
       [not found] ` <20200112100906.GA95942__47727.4053309642$1578823915$gmane$org@pooh.prefix.duckdns.org>
  1 sibling, 1 reply; 5+ messages in thread
From: Kusalananda Kähäri @ 2020-01-12 10:09 UTC (permalink / raw)
  To: Frank Gallacher; +Cc: zsh-users

On Sun, Jan 12, 2020 at 07:34:02PM +1100, Frank Gallacher wrote:
> I am converting my functions from bash to zsh, when I do something like:
> 
> 
> ciao.old () {
> 	if [ $# == 0 ]
> 	then
> 		PARM="-h" 
> 	else
> 		PARM=$* 
> 	fi
> 
> 
> I get an error like:
> 
> % ciao.old          
> ciao.old:8: = not found
> 
> 
> Using the old FORTRAN syntax, Replacing == with -eq fixes the problem; is this a bug, or just being tricky???
> 
> TIA, Frankie G.

== within [[ ]]
=  within [   ]

... just like in bash (but bash allows its built in test/[ utility to
understand == too)


-- 
Andreas (Kusalananda) Kähäri
SciLifeLab, NBIS, ICM
Uppsala University, Sweden

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

* SUMMARY Re: Use of == in functions
  2020-01-12 10:09 ` Kusalananda Kähäri
@ 2020-01-12 10:33   ` Frank Gallacher
  0 siblings, 0 replies; 5+ messages in thread
From: Frank Gallacher @ 2020-01-12 10:33 UTC (permalink / raw)
  To: Kusalananda Kähäri; +Cc: zsh-users

On 12/1/20, 9:09 pm, "Kusalananda Kähäri" <andreas.kahari@abc.se> wrote:

    On Sun, Jan 12, 2020 at 07:34:02PM +1100, Frank Gallacher wrote:
    > I am converting my functions from bash to zsh, when I do something like:
    > 
    > 
    > ciao.old () {
    > 	if [ $# == 0 ]
    > 	then
    > 		PARM="-h" 
    > 	else
    > 		PARM=$* 
    > 	fi
    > 
    > 
    > I get an error like:
    > 
    > % ciao.old          
    > ciao.old:8: = not found
    > 
    > 
    > Using the old FORTRAN syntax, Replacing == with -eq fixes the problem; is this a bug, or just being tricky???
    > 
    > TIA, Frankie G.
    
    == within [[ ]]
    =  within [   ]
    
    ... just like in bash (but bash allows its built in test/[ utility to
    understand == too)

Thanx Andreas,

(I stand corrected!)

Frank C.Gallacher
(franxg@gmail.com)

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

* Re: Use of == in functions
       [not found] ` <20200112100906.GA95942__47727.4053309642$1578823915$gmane$org@pooh.prefix.duckdns.org>
@ 2020-01-12 12:03   ` Stephane Chazelas
  2020-01-12 13:58     ` Daniel Shahaf
  0 siblings, 1 reply; 5+ messages in thread
From: Stephane Chazelas @ 2020-01-12 12:03 UTC (permalink / raw)
  To: zsh-users

[repost to zsh-users. I'll see if I can add a mutt hook to avoid
the problem in the future].

2020-01-12 11:09:06 +0100, Kusalananda Kähäri:
[...]
> == within [[ ]]
> =  within [   ]
> 
> ... just like in bash (but bash allows its built in test/[ utility to
> understand == too)
[...]

zsh's [ builtin also supports == as an alias of = (like its [[
]] construct also supports == as an alias of =), but in zsh,
=cmd is an operator that expands to the path of the cmd command,

$ echo =ls
/usr/bin/ls

so you would need:

[ a '==' b ]

(or disable the =cmd feature with set +o equals) if for some
reason you wanted to use the non-standard == in place of =.

Just like you need

[ a '=~' regex ]

for regex matching.

And

[ a '<' b ]

to compare strings lexically as < is also a redirection
operator.

Now, as none of <, ==, =~ are standard [ operators (so sh
compatibility is no longer a good reason to use the "["
command), you might as well use the ksh-style [[...]] construct
which doesn't have this kind of issue:

[[ a =~ b ]], [[ a < b ]], [[ a == b ]] are all fine (but then
again, there's no need to double the =. == is an operator that
is needed in languages where there's a need to disambiguate
between assignment and equality comparison, but inside [[...]]
(as opposed to ((...)) for instance), there's no assignment)


-- 
Stephane



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

* Re: Use of == in functions
  2020-01-12 12:03   ` Stephane Chazelas
@ 2020-01-12 13:58     ` Daniel Shahaf
  0 siblings, 0 replies; 5+ messages in thread
From: Daniel Shahaf @ 2020-01-12 13:58 UTC (permalink / raw)
  To: zsh-users; +Cc: Frank Gallacher

Stephane Chazelas wrote on Sun, 12 Jan 2020 12:03 +00:00:
> [repost to zsh-users. I'll see if I can add a mutt hook to avoid
> the problem in the future].
> 
> 2020-01-12 11:09:06 +0100, Kusalananda Kähäri:
> [...]
> > == within [[ ]]
> > =  within [   ]
> > 
> > ... just like in bash (but bash allows its built in test/[ utility to
> > understand == too)
> [...]
> 
> zsh's [ builtin also supports == as an alias of = (like its [[
> ]] construct also supports == as an alias of =), but in zsh,
> =cmd is an operator that expands to the path of the cmd command,
> 
> $ echo =ls
> /usr/bin/ls
> 
> so you would need:
> 
> [ a '==' b ]
> 
> (or disable the =cmd feature with set +o equals) if for some
> reason you wanted to use the non-standard == in place of =.
> 
> Just like you need
> 
> [ a '=~' regex ]
> 
> for regex matching.
> 
> And
> 
> [ a '<' b ]
> 
> to compare strings lexically as < is also a redirection
> operator.
> 
> Now, as none of <, ==, =~ are standard [ operators (so sh
> compatibility is no longer a good reason to use the "["
> command), you might as well use the ksh-style [[...]] construct
> which doesn't have this kind of issue:
> 
> [[ a =~ b ]], [[ a < b ]], [[ a == b ]] are all fine (but then
> again, there's no need to double the =. == is an operator that
> is needed in languages where there's a need to disambiguate
> between assignment and equality comparison, but inside [[...]]
> (as opposed to ((...)) for instance), there's no assignment)

The reason for the difference between «[ x == y ]» and «[[ x == y ]]» is
that [ is a _builtin_, so it's parsed using the same rules as any random
external command, whereas [[ is a _reserved word_, part of the syntax,
like '&&', so the normal command line syntax rules don't apply within
it.  This is also why «[[ -n foo && -n bar ]]» works

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

end of thread, other threads:[~2020-01-12 13:59 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-12  8:34 Use of == in functions Frank Gallacher
2020-01-12 10:09 ` Kusalananda Kähäri
2020-01-12 10:33   ` SUMMARY " Frank Gallacher
     [not found] ` <20200112100906.GA95942__47727.4053309642$1578823915$gmane$org@pooh.prefix.duckdns.org>
2020-01-12 12:03   ` Stephane Chazelas
2020-01-12 13:58     ` Daniel Shahaf

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).