Re: doesn't bash do variable subst. or quote removal on function statement
On 1/13/16 12:20 PM, Linda Walsh wrote: > It doesn't seem very consistent to choose (()) over [] -- why come up with > a special longer operator for arith eval, when [] was already used for > arith evaluation in array indexing? http://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xcu_chap02.html#tag_23_02_06_04 "In early proposals, a form $[expression] was used. It was functionally equivalent to the "$(())" of the current text, but objections were lodged that the 1988 KornShell had already implemented "$(())" and there was no compelling reason to invent yet another syntax. Furthermore, the "$[]" syntax had a minor incompatibility involving the patterns in case statements." Remember that Posix doesn't standardize arrays. -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/
Re: doesn't bash do variable subst. or quote removal on function statement
Chet Ramey wrote: On 1/12/16 4:33 PM, Linda Walsh wrote: --- But why? It's less noisy than any alternative. It's not as if I can type echo $(1+2) and expect it to work. Because it only ever appeared in a Posix draft standard, and there's no reason not to use the standard syntax. All of the linux security related to capabilities "only" appeared in a Posix draft standard. That hasn't stopped it from being adopted in various forms in many *nix's implementing capabilities. $() is already used for command execution and () is already used for expression grouping. Putting the two of them together takes 2 operators from 2 disparate areas and claiming it's a new operator vs. using $[] which AFAIK, isn't used for anything, seems deliberately Machiavellian, not to mention noisier. If both were available as a standard, which would make for clearer expressions? I.e. What can $[] be confused with? V. $() and () already having alternate uses, not to mention creating tortured, ambiguous syntax like: declare -i a=0;((a=1+2));(echo $(echo $a)) Now if I wanted to do the assignment in the echo, I have to do this?: declare -i a=0; (echo $(echo $((a=1+2 Vs. declare -i a=0; (echo $(echo $[a=1+2])) Of course the 2nd form using brackets, w/o the '$' can be used as an array subscript -- where it gets the same evaluation. I.e. declare -i a=0; (echo $(echo ${ar[a=1+2]})) -- i.e. the language already uses [] to hold an arithmetic expression for arrays, but you can't take $((a=1+2)) and use the expression after the $ directly as an array as in: declare -i a=0; (echo $(echo ${ar((a=1+2))})) (not that such usage would be that desirable -- but the point being that [] is already used for arith evaluation in arrays, so adding the sigil on front for stand-alone arithmetic evaluation seems to be a logical progression -- Not something one has with $(()). It doesn't seem very consistent to choose (()) over [] -- why come up with a special longer operator for arith eval, when [] was already used for arith evaluation in array indexing? *bleh*
Re: doesn't bash do variable subst. or quote removal on function statement
On 1/12/16 4:33 PM, Linda Walsh wrote: > > > Greg Wooledge wrote: >> On Tue, Jan 12, 2016 at 11:52:47AM -0800, Linda Walsh wrote: >> >>> Also, from a documentation standpoint, not all behaviors are documented >>> in the manpage, like: >>> >>> echo $[1+2] >>> >>> as far as I can tell, isn't documented, yet not supporting >>> its behavior would break many older scripts >>> >> >> That syntax has been deprecated for at least 15 years. > --- >But why? It's less noisy than any alternative. > It's not as if I can type echo $(1+2) and expect it to work. Because it only ever appeared in a Posix draft standard, and there's no reason not to use the standard syntax. -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/
Re: doesn't bash do variable subst. or quote removal on function statement
On 1/12/16 2:52 PM, Linda Walsh wrote: > > > Chet Ramey wrote: >> No. The shell doesn't perform any word expansions on the `name' when >> executing a function definition command. Since the documentation doesn't >> say it performs any expansions, why would you assume it does? >> > Because it does quote quote removal on other statements that _seem_ to be > of similar syntax. Appearances can be deceptive. The difference is between a simple command (that happens to be a builtin) and an element of the shell grammar. -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/
Re: doesn't bash do variable subst. or quote removal on function statement
Greg Wooledge wrote: On Tue, Jan 12, 2016 at 11:52:47AM -0800, Linda Walsh wrote: Also, from a documentation standpoint, not all behaviors are documented in the manpage, like: echo $[1+2] as far as I can tell, isn't documented, yet not supporting its behavior would break many older scripts That syntax has been deprecated for at least 15 years. --- But why? It's less noisy than any alternative. It's not as if I can type echo $(1+2) and expect it to work. Nobody is supposed to be using it. It's undocumented ON PURPOSE, to try to make people stop doing it. If you have older scripts that use it, fix them now. --- Only in 3rd party scripts where scripts come with some product to be used on *nix, which is why I worry about it. I wouldn't want those scripts to become my responsibility to "fix". Though, at one point, I seem to remember some wondering when systemd might integrate a shell-service as it has many other *nix utils (somewhere around the time it was integrating code to replace 'su' or 'sudo', I think). If that happens, then it's clear where compatibility will go.
Re: doesn't bash do variable subst. or quote removal on function statement
On Tue, Jan 12, 2016 at 11:52:47AM -0800, Linda Walsh wrote: > Also, from a documentation standpoint, not all behaviors are documented > in the manpage, like: > > echo $[1+2] > > as far as I can tell, isn't documented, yet not supporting > its behavior would break many older scripts That syntax has been deprecated for at least 15 years. Nobody is supposed to be using it. It's undocumented ON PURPOSE, to try to make people stop doing it. If you have older scripts that use it, fix them now.
Re: doesn't bash do variable subst. or quote removal on function statement
Chet Ramey wrote: No. The shell doesn't perform any word expansions on the `name' when executing a function definition command. Since the documentation doesn't say it performs any expansions, why would you assume it does? Because it does quote quote removal on other statements that _seem_ to be of similar syntax. I.e.: declare "ab=1" "ab"=1 ab"=1" a"b=1" "a"b=1 a"b"=1 ab"=1" all appear to do quote removal, making them equivalent to declare ab=1 vs. for function, where one has: function a {(echo hi)} then one might think, _at least_, that some similar syntax would work like: function "a" {(echo hi)} function "a" "{(echo hi)}" or even declare -f a="{(echo hi)}" _might_ work. Also, from a documentation standpoint, not all behaviors are documented in the manpage, like: echo $[1+2] as far as I can tell, isn't documented, yet not supporting its behavior would break many older scripts
Re: doesn't bash do variable subst. or quote removal on function statement
On 1/9/16 8:28 PM, Linda Walsh wrote: >Not in a function definition (i.e. this is broken AFAIAC in bash). It's not. > I.e. >> 'func' () { :; } > -bash: `'func'': not a valid identifier >> function 'func' () { : ; } > -bash: `'func'': not a valid identifier >> function "func" () { :; } > -bash: `"func"': not a valid identifier > --- > That's weird. while, _maybe_ I could see the 1st one not working > if looking for a bash keyword, but the 2nd & third forms seems odd. > I thought variable substitution and quote-removal was supposed > to happen before the statement is executed ?? No. The shell doesn't perform any word expansions on the `name' when executing a function definition command. Since the documentation doesn't say it performs any expansions, why would you assume it does? -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/
Re: doesn't bash do variable subst. or quote removal on function statement
Linda Walsh writes: >> shopt -s expand_aliases; alias my=declare >> declare fn=myfunc## function name in variable > doesn't work >> function $fn { echo $fn ; } > -bash: `$fn': not a valid identifier >>my -pf myfunc > -bash: declare: myfunc: not found >> >> def="function $fn () { echo $fn ; }" ## but same statement, eval'd works That's not the same statement. The same statement would be def='function $fn () { echo $fn ; }' Andreas. -- Andreas Schwab, sch...@linux-m68k.org GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5 "And now for something completely different."
doesn't bash do variable subst. or quote removal on function statement
Reuti wrote: Am 08.01.2016 um 21:36 schrieb Piotr Grzybowski: hello Linda, On Fri, Jan 8, 2016 at 9:14 PM, Linda Walsh wrote: For what it's worth, It might be useful to have something like 'command' [..] that would be useful, a keyword like: function p params; But 'function p'... is already a lead-in to a function definition, i.e: function xyz { :; } #(create function foo) AFAICS putting the name in quotes (single or double) will prefer the function over the alias, but it will fall back to a command if there is no function. 'P' params Not in a function definition (i.e. this is broken AFAIAC in bash). I.e. 'func' () { :; } -bash: `'func'': not a valid identifier function 'func' () { : ; } -bash: `'func'': not a valid identifier function "func" () { :; } -bash: `"func"': not a valid identifier --- That's weird. while, _maybe_ I could see the 1st one not working if looking for a bash keyword, but the 2nd & third forms seems odd. I thought variable substitution and quote-removal was supposed to happen before the statement is executed ?? I.e.: this _seems_ broken: shopt -s expand_aliases; alias my=declare declare fn=myfunc## function name in variable doesn't work function $fn { echo $fn ; } -bash: `$fn': not a valid identifier my -pf myfunc -bash: declare: myfunc: not found def="function $fn () { echo $fn ; }" ## but same statement, eval'd works eval $def my -f myfunc myfunc () { echo myfunc } I.e. while having the function name in a variable doesn't work interactively, it does work when used with eval.