Re: Shell Grammar man page function definition
On 2/28/21 9:56 PM, Dale R. Worley wrote: What does not work: function x ( : ) Check your Bash version. IIRC, recent versions (e.g. 5.1) have a minor change in the Bison grammar (parse.y) for function definitions, IIRC that I provided. This is right; Dale sent the patch. -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/
Re: Shell Grammar man page function definition
On Sun, Feb 28, 2021 at 05:06:37PM -0500, Chet Ramey wrote: > On 2/28/21 12:38 PM, Mike Jonkmans wrote: > > > The manual page says: > > If the function reserved word is used, but the parentheses are not > > supplied, > > the braces are required. > > That text has been there since the earliest versions of the man page. It's > not strictly true any more, but it's a reasonable guideline. Maybe I'll > change the language to a recommendation. I think deleting , with one exception: If the function reserved word is used, but the parentheses are not supplied, the braces are required would suffice. > > But it seems that from all the compound commands, > > only a subshell is not possible. > > Subshells work fine in the current version of bash. Nice! Thanks, also to the others to point that out. I did not see that in the changelog (master-branch) though. But the 'culprit' is clear from the next post ;) Regards, Mike Jonkmans
Re: Shell Grammar man page function definition
Mike Jonkmans writes: > Some examples that work: > function x { :; } ## as expected > function x if :; then :; fi > function x (( 42 )) > function x [[ 42 ]] > function x for x do :; done > function x for (( ; 42 - 42 ; )); do :; done > > What does not work: > function x ( : ) Check your Bash version. IIRC, recent versions (e.g. 5.1) have a minor change in the Bison grammar (parse.y) for function definitions, IIRC that I provided. The purpose was to stop Bison from giving an annoying "parse conflict" message when compiling the grammar, but looking at it, it should allow your example to work, because it gets the parser to handle all the cases as they are specified in the manual page. Dale
Re: Shell Grammar man page function definition
On 2/28/21 12:38 PM, Mike Jonkmans wrote: The manual page says: If the function reserved word is used, but the parentheses are not supplied, the braces are required. That text has been there since the earliest versions of the man page. It's not strictly true any more, but it's a reasonable guideline. Maybe I'll change the language to a recommendation. But it seems that from all the compound commands, only a subshell is not possible. Subshells work fine in the current version of bash. -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/
Re: Shell Grammar man page function definition
28 Şubat 2021 Pazar tarihinde Mike Jonkmans yazdı: > Hi, > > The manual page says: > If the function reserved word is used, but the parentheses are not > supplied, > the braces are required. > > But it seems that from all the compound commands, > only a subshell is not possible. > > Some examples that work: > function x { :; } ## as expected > function x if :; then :; fi > function x (( 42 )) > function x [[ 42 ]] > function x for x do :; done > function x for (( ; 42 - 42 ; )); do :; done > > What does not work: > function x ( : ) Works fine here. bash-5.1$ function x ( : ) bash-5.1$ declare -f x x () { ( : ) } > > > From looking at the bash grammar I am not sure why a subshell does not > work. > As the subshell can be differentiated from the optional parentheses. > > > Though it may be handy to disallow a subshell. > If in the future named parameters would become a possibility. > E.g. > function x (a=$1 b=$2) > { echo "$a$b; } > would become ambiguous. > > > Note that the Posix grammar doesn't mention the word 'function'. > Posix does mention it, as reserved word recognized by 'some > implementations' > and causing undefined behavior. > > > Regards, Mike Jonkmans > > -- Oğuz
Re: Shell Grammar man page function definition
Mike Jonkmans (bash...@jonkmans.nl) wrote: > What does not work: > function x ( : ) The parser is looking for () after the function name. Most likely, the opening ( is confusing it. unicorn:~$ bash unicorn:~$ x() ( : ) unicorn:~$ function y() ( : ) unicorn:~$ type x x is a function x () { ( : ) } unicorn:~$ type y y is a function y () { ( : ) }
Shell Grammar man page function definition
Hi, The manual page says: If the function reserved word is used, but the parentheses are not supplied, the braces are required. But it seems that from all the compound commands, only a subshell is not possible. Some examples that work: function x { :; } ## as expected function x if :; then :; fi function x (( 42 )) function x [[ 42 ]] function x for x do :; done function x for (( ; 42 - 42 ; )); do :; done What does not work: function x ( : ) >From looking at the bash grammar I am not sure why a subshell does not work. As the subshell can be differentiated from the optional parentheses. Though it may be handy to disallow a subshell. If in the future named parameters would become a possibility. E.g. function x (a=$1 b=$2) { echo "$a$b; } would become ambiguous. Note that the Posix grammar doesn't mention the word 'function'. Posix does mention it, as reserved word recognized by 'some implementations' and causing undefined behavior. Regards, Mike Jonkmans