Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 35 down vote accepted

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.

share|improve this answer
You can workaround the argument issue on aliases eg: alias mkcd='_mkcd(){ mkdir "$1" && cd "$1"; }; _mkcd' – chilicuil Feb 21 at 20:59
@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 at 21:06
for hundred of aliases I prefer this approach due to be easier to look for them (alias will list all of them), also I think it's slighly faster – chilicuil Feb 21 at 21:29
@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 at 21:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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