What an alias really does
An alias replaces the first word of a command with the text you defined. The replacement happens at the time bash reads the line, before it parses the rest of the command. Because it is plain text substitution, an alias cannot take arguments in the middle of its definition. Any arguments you type after the alias name are appended to the replacement text.
alias ll="ls -l"
ll /workspaceBash expands ll into ls -l and then runs ls -l /workspace. The alias name is not a command on its own. You can see the definition with alias ll, and you can remove it with unalias ll.
When aliases are active
Alias expansion is turned on for interactive shells and turned off for scripts. This is a deliberate safety choice. A script should behave the same way regardless of what its author put in a personal file. If you need expansion in a script, you must turn it on with shopt -s expand_aliases, and the alias must be defined on an earlier line than the one that uses it, because bash reads a script one command at a time.
An alias whose text ends in a space also causes the next word to be checked for alias expansion. This is a small rule, but it explains why some chained aliases work and others do not.
Bypassing an alias
Sometimes you want the real command rather than the alias. Prefix the name with a backslash, as in \ls, to skip the alias. The command builtin also skips aliases and functions, so command ls runs the external program. Both forms are useful when you are debugging a shortcut that does something unexpected.
What a function is
A function is a named compound command. It has its own positional parameters, so $1 inside the function refers to the first argument of the call, not the first argument of the script. It runs in the current shell, so variables it changes stay changed after it returns. The return command ends the function and sets its exit status.
shout() { echo "$1!"; }
shout helloThe output is hello!. The function works in scripts and in interactive shells, and it does not depend on alias expansion being turned on.
Declare variables that belong to a function with local. Without it, a variable assigned inside a function changes the variable of the same name in the caller.
Checking which one you have
type tells you what a name refers to. It prints a line such as shout is a function when the name is a function, and it prints the alias definition when the name is an alias. The -t option prints just one word, which is convenient in scripts. The unset -f command removes a function, and it is the function equivalent of unalias.
Scope and lifetime
An alias lives in the shell that defines it, and it is gone when that shell exits. Put it in ~/.bashrc to make it available in every interactive shell. A function has the same lifetime, but a wider reach: it can read and change variables of the calling shell, it takes arguments, and a script that sources the file that defines it can call it. A change to ~/.bashrc does not reach a shell that is already open until you read the file again.
When you read a function that someone else wrote, first find its definition, then find every place that calls it. The call sites show what the function is expected to receive, and they often reveal that a function is really a short script with a clearer name. That reading habit is the fastest way to understand a large dotfile.
A function is local to the shell that defines it, but you can make it visible to child bash processes with export -f. The child then inherits the definition through the environment, which is useful when a script started from your shell must call the same helper. Use it sparingly, because an exported function is one more name that a program can find without a file on disk.
Defining a function with the name of one you already have replaces the old definition without a warning. This is convenient while you experiment, and it is a risk in a large startup file where two parts of the file define the same name.
Arguments and return values
Inside a function, $1, $2, and $@ refer to the arguments passed to that function, and $# gives their count. The return command sets a status, which is an integer from 0 to 255. It does not return text. To pass text back, the function writes to standard output, and the caller captures that output with command substitution. Keeping this rule in mind avoids the common mistake of expecting return to work like a value in a programming language.
Choosing between them
Use an alias for a short typed form of a command that already works well with no arguments of its own, such as ll or a flag you always want. Use a function when the shortcut needs arguments in the middle, a loop, a condition, or a value that must be used later. Use a script when the code needs to be shared with other users or run by other tools. The rule of thumb is simple. A shortcut that you type is an alias. A small program that you call is a function.