36

This site says, "Shell functions are faster [than aliases]. Aliases are looked up after functions and thus resolving is slower. While aliases are easier to understand, shell functions are preferred over aliases for almost every purpose."

Given that (true or not), how do shell functions compare to standalone shell scripts? Does one have particular advantages over the other, or better suited for certain types of tasks?

46

The main difference between aliases and functions is that aliases don't take arguments¹, but functions do. When you write something like alias l='ls --color', l foo is expanded to ls --color foo; you can't grab foo into the alias expansion and do something different with it the way you can do with a function. See also How to pass parameter to alias?.

Aliases are looked up before functions: if you have both a function and an alias called foo, foo invokes the alias. (If the alias foo is being expanded, it's temporarily blocked, which makes things like alias ls='ls --color' work. Also, you can bypass an alias at any time by running \foo.) I wouldn't expect to see a measurable performance difference though.

Functions and standalone scripts have mostly similar capabilities; here are a few differences I can think of:

  • A function runs inside the shell environment; a script runs in a separate process. Therefore a function can change the shell environment: define environment variables, change the current directory, etc. A standalone script can't do that.
  • A function must be written in the language of the shell you want to use it in. A script can be written in any language.
  • Functions are loaded when they are defined. Scripts are loaded each time they are invoked. This has several consequences:
    • If you modify a script, you get the new version the next time you invoke it. If you change a function's definition, you have to reload the definition.
    • Functions are faster on heavily loaded systems.
    • If you have a lot of functions that you may not use, they'll take up memory. Ksh and zsh, but I think not bash, have a form of function autoloading.

Something that's intermediate between a function and a standalone script is a script snippet that you read with the source or . builtin. Like a function, it can modify the shell's environment, and must be written in the shell's language. Like a script, it is loaded each time it's invoked and no sooner.

¹ Yeah, I know, this doesn't apply to tcsh.

  • 1
    You can workaround the argument issue on aliases eg: alias mkcd='_mkcd(){ mkdir "$1" && cd "$1"; }; _mkcd' – Javier López Feb 21 '16 at 20:59
  • 1
    @chilicuil Why would you define an alias that redefines a function each time it's called? Why not define the function once and for all, which makes the alias be just alias mkcd=_mkcd, which is pointless as you could just have called the function mkcd in the first place? – Gilles Feb 21 '16 at 21:06
  • 1
    @chilicuil Uh? It's slightly (but not significantly) slower (obviously, since there's extra work compared to the direct way), and you can list functions just as easily. – Gilles Feb 21 '16 at 21:46
  • 1
    @anthony I don't recommend replacing an alias by a function. It's possible, but has no advantages, only downsides. An example of something that can go wrong is if function b calls a, and a starts out as an alias, then the alias is expanded when a is defined; if a is later replaced by a function, b will not call the function. To implement function autoloading, do it the ksh and zsh way: replace a stub function by the real function. – Gilles Nov 15 '16 at 12:25
  • 1
    @OlivierDulac It's always aliases before functions. Note that an alias definition only takes effect at the end of a line, not after a ;. command ls portably bypasses both aliases and functions. – Gilles Apr 20 '17 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.