Re: What shell feature did surprise you?



2011-05-27, 14:09(+01), Geoff Clare:
Stephane CHAZELAS wrote:

2011-05-27, 13:01(+02), Janis Papanagnou:

Just remembered... - I had been surprised by a function definition
with (subshell-)parenthesis instead of curly braces around the
function body ; with all consequences, as in the subsequent example...

In all Bourne-like shells but bash, the syntax is

foo() any-command

so can be

foo() echo test
f() g() { echo test; }
foo() { ...; }
foo() ( ...; )
foo() for i do
echo "<$i>"; done

bash and POSIX only allow compound commands (so all but the
first 2 above).

In POSIX the syntax is:

fname() compound-command[io-redirect ...]

I can remember being surprised about that io-redirect when I came
across it in the standard in the mid 1990's.

$ ls
$ func() { echo foo; } > "$func_out"
$ func_out=file1; func
$ func_out=file2; func
$ ls
file1 file2

Yes, and I remember now that zsh is not conformant there as it
performs the redirection at the time of the function definition
in the f() { echo a; } > x
http://www.zsh.org/mla/workers/2008/msg01674.html

So it's even less clear why POSIX doesn't allow
f() echo x
would you know?

--
Stephane
.



Relevant Pages

  • Re: What shell feature did surprise you?
    ... foo() any-command ... fg{echo test;} ... bash and POSIX only allow compound commands (so all but the ... commands and not other commands I don't know, ...
    (comp.unix.shell)
  • Re: What shell feature did surprise you?
    ... - I had been surprised by a function definition ... foo() any-command ... In POSIX the syntax is: ... $ func_out=file1; func ...
    (comp.unix.shell)
  • Re: What Data Structure should I use?
    ... I have a function called foo() and it accepts objects of classes as ... how do I define the parameter in the function definition? ... public void foo(Ftr paramname) {... ...
    (comp.lang.java.programmer)
  • Re: saving/restoring function def (bash)
    ... How can I save existing function definition of foo ... redefine foo() differently but keep the old definition around). ... it involves a fork. ...
    (comp.unix.shell)
  • Re: saving/restoring function def (bash)
    ... How can I save existing function definition of foo ... redefine foo() differently but keep the old definition around). ... echo FOO ...
    (comp.unix.shell)