Alias Command in Linux: Create and Use Shortcuts

Alias Command in Linux: Create and Use Shortcuts

A command you use often can be awkward to type whenever you open a terminal. The Linux alias command lets a shell replace a short name with fixed command text.

This piece explains how to create an alias and keep it available in later Bash sessions.

TL;DR

The alias command in Linux gives a shell a short name for fixed command text. In Bash, define one with an equals sign and no surrounding spaces.

  • Create a session alias with alias ll=’ls -lh’.
  • Inspect definitions with alias or one name with alias ll.
  • Put persistent Bash aliases in ~/.bashrc.
  • Use a shell function when the command needs arguments.

What is an alias in Linux?

I checked the Bash manual, which describes an alias as a shell substitution applied when Bash reads a command. Its definition lives in the active shell’s alias table.

That boundary matters when debugging: inspect the alias table in the shell that is showing the unexpected behavior.

How to create and use an alias step by step

Keep the familiar ls name unchanged by choosing a distinct shortcut such as ll.

Step 1: Define a temporary alias

Write the alias name beside the equals sign with no spaces, then quote the replacement text.

alias ll='ls -lh'

Bash reads and expands input before execution, so put an alias definition on its own line before you use it.

Step 2: Inspect and run the alias

Ask Bash to print the definition, then type its name as a command. This local demo uses two small text files to make the expanded listing visible.

alias ll='ls -lh alias-demo-data'
alias ll
ll

I ran the source file in an interactive Bash shell with this command:

bash -ic 'source alias-demo.sh'

The terminal printed the definition and the two-file listing. The job-control warnings come from launching an interactive shell without a controlling terminal in this capture. The command still returned exit status zero.

ninad@linuxfordevices:~$ bash -ic 'source alias-demo.sh'
bash: cannot set terminal process group (571392): Inappropriate ioctl for device
bash: no job control in this shell
alias ll='ls -lh alias-demo-data'
total 8.0K
-rw-rw-r-- 1 ubuntu ubuntu 4 Oct 19 23:35 notes.txt
-rw-rw-r-- 1 ubuntu ubuntu 4 Oct 19 23:35 readme.txt
Bash prints the ll alias definition and a listing of two files

The first printed line confirms what Bash will substitute. The following lines are the output from the replacement command, not a separate alias feature.

Step 3: Save a Bash alias for later sessions

Add the definition on its own line in your Bash startup file. Interactive non-login Bash reads ~/.bashrc when that file exists.

alias ll='ls -lh'

Sourcing reads the file in the current shell, so the new alias becomes available there without opening another terminal.

source ~/.bashrc

A login shell may read a profile file instead, depending on how Bash starts.

Step 4: List or remove aliases

Run alias without a name to print all definitions. Give it one name to inspect that entry, or use unalias to remove a temporary definition.

alias
alias ll
unalias ll

Removing an active definition does not remove its saved line from ~/.bashrc. Delete that line as well if the alias should stay absent in future sessions. Bash also accepts unalias -a to clear every alias in the current shell.

Alias limits and common mistakes

Because aliases have no argument placeholders, functions are the better fit for commands that take positional arguments or conditional logic. The Bash manual recommends functions for almost every purpose.

mkcd() {
  mkdir -p -- "$1" && cd -- "$1"
}

This function uses its first argument as a directory name. It creates the directory and changes the current shell’s location, which an external program cannot do for its parent shell.

Aliases are generally disabled in non-interactive Bash scripts. Call the command directly in a script instead of relying on an alias loaded by an interactive shell.

Bash checks an alias in command position, usually at the start of a simple command. If an alias value ends with a blank, Bash also checks the next word for another alias. That chaining behavior is specialized, so keep definitions simple and inspect them when parsing is unexpected.

type ll

Conclusion: Use aliases for fixed shortcuts

When a name resolves unexpectedly, check its active definition with alias or type. For more shell context, see our Linux command-line guide and terminal aliases tutorial.

FAQ

The FAQ answers cover recurring alias questions.

How do I make an alias permanent in Bash?

Add its definition to ~/.bashrc, then run source ~/.bashrc in the current interactive Bash session or open a new session.

How do I see aliases in the current shell?

Run alias to list all definitions, or alias name to inspect one definition.

Can a Bash alias take arguments?

Aliases do not provide positional parameters. Define a shell function when the command needs arguments or conditional behavior.