$_ CLI In Depth
Learning progress 0%
lesson environment path dotfiles

Aliases vs Functions

5 min read Module 9: Environment, PATH & Dotfiles Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:17

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.

BASH2 lines
alias ll="ls -l"
ll /workspace

Bash 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.

BASH2 lines
shout() { echo "$1!"; }
shout hello

The output is hello!. The function works in scripts and in interactive shells, and it does not depend on alias expansion being turned on.

Note

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.

Terminal lab · 2 steps · real Linux in your browser

Aliases and functions in practice

Call a function with an argument, and see why an alias in a script needs shopt -s expand_aliases.

01
Call a function with an argument
Create fn-demo.sh in /workspace. It defines shout, calls it with hello, and then prints what kind of name shout is: ``bash #!/bin/bash shout() { echo "$1!"; } shout hello type -t shout ` Run bash fn-demo.sh. The output is hello! and then function`.
Show hint for step 1
Inside the function, $1 is the first argument of this call.
02
Make an alias work in a script
Create alias-demo.sh in /workspace. It defines an alias, calls it once before turning on alias expansion, and calls it again after: ``bash #!/bin/bash alias greet="echo from-alias" greet shopt -s expand_aliases greet ` Run bash alias-demo.sh. The first call prints an error on standard error and nothing on standard output. The second call prints from-alias`.
Show hint for step 2
Alias expansion is off in scripts by default. shopt -s expand_aliases turns it on for the lines after it.
Check yourself Question 1 of 4
Concept Check Bypassing an alias recall

Which form runs the real ls command when an alias named ls exists?

Recap & Key Takeaways

An alias is a text substitution that is active in interactive shells and off in scripts by default.
A function receives its own positional parameters and runs in the current shell.
Use \name or command name to bypass an alias, and use type to see what a name refers to.
Use local inside a function to keep variables from leaking into the caller.

Primary Sources & Further Reading

Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue