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.
alias mkcd='_mkcd(){ mkdir "$1" && cd "$1"; }; _mkcd'– Javier López Feb 21 '16 at 20:59alias mkcd=_mkcd, which is pointless as you could just have called the functionmkcdin the first place? – Gilles 'SO- stop being evil' Feb 21 '16 at 21:06Autoloading is basically similar. Functions are defined if a files of that name exists in a special directory. Then on first call, that function replaces itself with the actual function from the file of the same name. And BASH can do this, if you really want to do it. I have seen autoloaders for bash. They are just not often used.
(PS the 5 minute time limit on comment edits on this forum is too short!
– anthony Nov 15 '16 at 05:52bcallsa, andastarts out as an alias, then the alias is expanded whenais defined; ifais later replaced by a function,bwill 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' Nov 15 '16 at 12:25functions before alias before command_via_$PATH. In bash: try:alias ls='/bin/ls -d ' ; function ls { /bin/ls -alF "$@" /dev/null ;}and : ls /dev/tty : it will show it using the function, showing thus both /dev/tty and /dev/null. if you delete the function, it will use the alias (ie, with ls -d). If you do :"ls"or\lsit will bypass the alias, but if there is a function it still uses it as it is used first. To bypass both alias AND function (in bash) use :command lsto get to launch the firstlsfound in the $PATH (or in builtins). – Olivier Dulac Apr 20 '17 at 15:42;.command lsportably bypasses both aliases and functions. – Gilles 'SO- stop being evil' Apr 20 '17 at 16:34