Groups | Search | Server Info | Login | Register


Groups > comp.unix.shell > #26832

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-12 19:52 +0200
Organization A noiseless patient Spider
Message-ID <83fr3wtt0r.fsf@helmutwaitzmann.news.arcor.de> (permalink)
References <slrn11062qn.n9t.hymie@nasalinux.net>

Show all headers | View raw


 hymie! <hymie@nasalinux.net>:
> 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. 

 […]

>
> Other thoughts?  (or facts?) 
>

 It might be very hard or even impossible to make a shell function 
 free of its context.  For example, a shell function might iterate 
 over its given positional parameters: 


   sum()
   (
     unset -v sum arg &&
     sum=0 &&
     for arg
     do
       sum="$(( sum + arg ))"
     done &&
     printf '%s\n' "$sum"
   )


 Now, if one invokes it like 


    readonly arg sum && sum 1 2 3


 it will fail. 


 In this particular example, though, that problem can be avoided: 


    sum()
    {
      if ! : ${1+:} false
      then
        set -- 0
      else
        while ${2+:} false
        do
          set -- "$@" "$(( ${1} + ${2} ))"
          shift 2
        done
      fi
      printf '%s\n' "$1"
    }


 On the other hand, a shell script of its own, like 


   #!/bin/sh
   sum()
   {
     sum=0 &&
     for arg
     do
       sum="$(( sum + arg ))"
     done &&
     printf '%s\n' "$sum"
   } &&
   sum "$@"


 won't fail. 

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? 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