This script demonstrates defining a bash function with parenthesis verses with braces. The parenthesis have the nice effect of making environment variables created in the function "local", I guess because the function body is executed as a sub-shell. The output is:
A=something
A=
B=something
B=something
The question is if this is allowed syntax for defining a function.
#!/bin/bash
foo() (
export A=something
echo A=$A
)
bar() {
export B=something
echo B=$B
}
foo
echo A=$A
bar
echo B=$B
Both are valid, and as Carl mentioned any compound command can also be used, e.g.:
POSIX 7 2.9.5 Function Definition Command http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_05:
Then 2.9.4 Compound Commands http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_04:
The semantics are the same as using
()
without a function definition: it does not create a new process, but gets executed in what POSIX and Bash call a "subshell environment".Yes, that syntax is allowed. As described in the
bash
man page, the definition of a bash function is:Some more description (also from the man page):
()
and{}
enclosed lists are compound commands. The full list (again from the man page, just edited down to a simple list):