Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.unix.shell > #26846

Re: shell function or shell script?

From Helmut Waitzmann <nn.throttle@erine.email>
Newsgroups comp.unix.shell
Subject Re: shell function or shell script?
Date 2026-05-14 19:03 +0200
Organization A noiseless patient Spider
Message-ID <83lddlsz3o.fsf@helmutwaitzmann.news.arcor.de> (permalink)
References <slrn11062qn.n9t.hymie@nasalinux.net> <83fr3wtt0r.fsf@helmutwaitzmann.news.arcor.de> <10u0gho$1l93k$11@dont-email.me> <83pl2zrodb.fsf@helmutwaitzmann.news.arcor.de> <10u3108$1l93l$35@dont-email.me>

Show all headers | View raw


 Janis Papanagnou <janis_papanagnou+ng@hotmail.com>:
> On 2026-05-13 23:28, Helmut Waitzmann wrote:
>> The problem I presented is not related to getopts but to the 
>> fact that a readonly variable neither can be assigned a new 
>> value nor can be unset – even if the assignment resp. unset 
>> statement occurs in a subshell environment. 
>>
>>
>> Are you going to say that that won't fail using Kornshell?  So, 
>> what would be the working Kornshell equivalent to the 
>> not‐working command 
>>
>>
>>    (
>>      sum()
>>      (
>>        sum=0 &&
>>        for arg
>>        do
>>          sum="$(( sum + arg ))"
>>        done &&
>>        printf '%s\n' "$sum"
>>      ) &&
>>      readonly arg sum && sum 1 2 3
>>    )
>>
>> when given to a POSIX‐Shell? 
>>
>
> Trying to change a read-only variable must of course fail since 
> it's declared 'readonly'. But it makes little sense guessing what 
> your intention here is. 
>

 My intention is the one of the OP, as hymie! 
 <hymie@nasalinux.net> wrote: 


 “So typically, when I need to do something a little complex, I 
 will write a shell script. 


 My boss, on the other hand, will write a shell function that he 
 sources into his shell. 


 I'm curious about the pros/cons of each method.” 


 The example (summing up the positional parameters by means of a 
 “for” loop) showed a usecase where some implementation using a 
 shell function (the boss' way) will fail when used in an invoking 
 shell environment where the shell variable “arg” or “sum” happens 
 to be a readonly variable. 


 On the other hand, the implementation using a shell script 
 (hymie!'s way) will succeed, even when used in an invoking shell 
 environment where the shell variable “arg” or “sum” happens to be 
 a readonly variable. 


> Initially you had said: "For example, a shell function might 
> iterate over its given positional parameters [...]" 
>
> And that is no problem since you use a Kornshell function with 
> its own scope. 
>

 It is no problem with a POSIX shell as well:  Each function has 
 got its own scope regarding the positional parameters. 


> Summing up values passed to a function, in ksh, I'd just do 
>
>
>      function sumup
>      {
>        typeset sum=0  # a local variable
>        typeset arg    # a local variable
>        for arg
>        do
>          (( sum += arg ))
>        done
>        printf '%d\n' sum
>      }
>
>      readonly sum sum   # no problem but unnecessary - just remove that
>      # declaration


 No.  Removing that declaration is not an option, as it is part of 
 the invoking shell environment which is neither known to nor in 
 charge of the “sumup” implementor. 


>      # irrelevant since you'd work with local vars in sumup
       # and it makes no sense to have the variables in sumup as r/o
>      sumup 1 2 3
>
> I think the code is clear enough and don't see where you see a 
> problem. 
>
>
> If that's not what had been your point, please formulate more 
> clearly what you actually want to achieve. 


 If you had bothered to run the given example you would have seen 
 the shell's error messages stating that the readonly attribute is 
 the problem rather than any “local” variable (which is by the way 
 not part of the POSIX standard and in this case unnecessary, as 
 the function creates a subshell environment of its own by means 
 of the function definition using parentheses for the body of the 
 function). 


 The problem:  The implementor of the “sumup” function doesn't 
 have any means to remove the readonly attribute of any variable 
 defined in the invoking shell environment which happens by 
 coincidence to have the same name like a variable used in the 
 “sumup” function. 


 I gave that example using a “for” loop for summing up the 
 positional parameters to show that in some cases there might 
 exist an implementation (here: using a “while” loop which doesn't 
 make use of any shell variables besides of the positional 
 parameters) which doesn't suffer from the “readonly” attribute 
 inherited from the invoking shell environment. 


> (And please also try to formulate the code without unnecessary 
> irritating obfuscations as naming a function and a variable by 
> the same name, even if it's functionally no problem.) 
>

 A variable reference is clearly distinguishable from a function 
 reference by the presence of either a “=” (when assigning) or a 
 “$” (when evaluating).  So, which lines of the given example were 
 unclear to you? 

Back to comp.unix.shell | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

shell function or shell script? hymie! <hymie@nasalinux.net> - 2026-05-12 11:13 +0000
  Re: shell function or shell script? gmc@metro.cx (Koen Martens) - 2026-05-12 16:13 +0000
  Re: shell function or shell script? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-05-12 18:26 +0200
    Re: shell function or shell script? Christian Weisgerber <naddy@mips.inka.de> - 2026-05-12 18:46 +0000
      Is a shell function body consisting of other than a grouping command syntactically valid? (was: shell function or shell script?) Helmut Waitzmann <nn.throttle@erine.email> - 2026-05-12 23:45 +0200
        Re: Is a shell function body consisting of other than a grouping command syntactically valid? (was: shell function or shell script?) Christian Weisgerber <naddy@mips.inka.de> - 2026-05-13 13:33 +0000
      Re: shell function or shell script? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-05-13 02:18 +0200
        Re: shell function or shell script? Christian Weisgerber <naddy@mips.inka.de> - 2026-05-13 13:44 +0000
          Re: shell function or shell script? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-05-14 01:22 +0200
      Re: shell function or shell script? Geoff Clare <geoff@clare.See-My-Signature.invalid> - 2026-05-13 13:34 +0100
        Random ramblings about various shell stuff (Was: [long ago and far away] shell function or shell script?) gazelle@shell.xmission.com (Kenny McCormack) - 2026-05-13 12:52 +0000
  Re: shell function or shell script? Helmut Waitzmann <nn.throttle@erine.email> - 2026-05-12 19:52 +0200
    Re: shell function or shell script? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-05-13 02:27 +0200
      Re: shell function or shell script? Helmut Waitzmann <nn.throttle@erine.email> - 2026-05-13 23:28 +0200
        Re: shell function or shell script? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-05-14 01:20 +0200
          Re: shell function or shell script? Helmut Waitzmann <nn.throttle@erine.email> - 2026-05-14 19:03 +0200
            Re: shell function or shell script? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-05-15 04:10 +0200
  Re: shell function or shell script? Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-05-12 21:16 +0000
    Re: shell function or shell script? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-05-13 02:33 +0200

csiph-web