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?
1 Answer 1
The main difference between aliases and functions is that aliases don't take arguments1, 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.
1 Yeah, I know, this doesn't apply to tcsh.
-
1You can workaround the argument issue on aliases eg:
alias mkcd='_mkcd(){ mkdir "1ドル" && cd "1ドル"; }; _mkcd'
Javier López– Javier López2016年02月21日 20:59:45 +00:00Commented Feb 21, 2016 at 20:59 -
2@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 functionmkcd
in the first place?Gilles 'SO- stop being evil'– Gilles 'SO- stop being evil'2016年02月21日 21:06:43 +00:00Commented Feb 21, 2016 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 'SO- stop being evil'– Gilles 'SO- stop being evil'2016年02月21日 21:46:48 +00:00Commented Feb 21, 2016 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
callsa
, anda
starts out as an alias, then the alias is expanded whena
is defined; ifa
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 'SO- stop being evil'– Gilles 'SO- stop being evil'2016年11月15日 12:25:42 +00:00Commented Nov 15, 2016 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 'SO- stop being evil'– Gilles 'SO- stop being evil'2017年04月20日 16:34:25 +00:00Commented Apr 20, 2017 at 16:34