A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://unix.stackexchange.com/questions/4023/bash-functions-vs-scripts below:

bash - Aliases vs functions vs scripts

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:

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.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4