Bash Aliases

An alias is a short nickname for a longer command. Typing ll instead of ls -alh saves effort across hundreds of daily commands. Aliases live in the shell and customize the terminal to match your habits.

The Phone Contact Analogy

A phone stores a long number under a short name such as "Mom". Tapping the name dials the full number. An alias stores a long command under a short word in the same way.

   You type:   ll
                |
                v
   Bash looks up the alias table
                |
                v
   Bash runs:  ls -alh

Create a Temporary Alias

alias ll='ls -alh'
ll

The alias works until the terminal window closes.

View and Remove Aliases

alias            # list every alias
alias ll         # show one alias
unalias ll       # remove an alias

Helpful Aliases

alias ..='cd ..'
alias ...='cd ../..'
alias c='clear'
alias h='history'
alias gs='git status'
alias update='sudo apt update && sudo apt upgrade'
alias myip='hostname -I'
alias cls='clear'

Safety Aliases

Add the interactive flag to ask a question before overwriting or deleting:

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

Make Aliases Permanent

Save aliases inside the startup file ~/.bashrc. Bash reads this file each time a new terminal opens.

nano ~/.bashrc

Add lines at the bottom:

# My custom aliases
alias ll='ls -alh'
alias ..='cd ..'

Reload the file without closing the window:

source ~/.bashrc

Keep Aliases in a Separate File

Large collections stay tidy in their own file. Bash starts from the .bashrc file, which can load the alias file with a short block:

if [ -f ~/.bash_aliases ]; then
  . ~/.bash_aliases
fi

Ubuntu already includes this block by default.

Which Startup File Does What?

FileWhen Bash Reads It
~/.bashrcEvery new interactive terminal window
~/.bash_profileLogin sessions, such as remote logins
~/.bash_aliasesLoaded by .bashrc on many systems
~/.bash_logoutWhen a login session ends

Alias Limits and Functions

An alias cannot take arguments in the middle of a command. A function solves that problem:

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

Typing mkcd projects creates the folder and enters it. Place the function in ~/.bashrc next to your aliases.

Bypass an Alias

A backslash runs the original command without the alias:

\rm old.txt

Custom Prompt

The PS1 variable controls the text before each command. Add a line to ~/.bashrc for a personal look:

export PS1="\u@\h:\w\$ "

The codes \u, \h, and \w show the username, computer name, and current folder.

Practical Example: Starter Alias Set

Copy this block into ~/.bashrc for a comfortable terminal:

alias ll='ls -alh'
alias la='ls -A'
alias ..='cd ..'
alias grep='grep --color=auto'
alias df='df -h'
alias du='du -h'
alias ports='ss -tuln'
alias now='date +"%Y-%m-%d %H:%M"'

Each line shortens a command that people type often. The color option on grep highlights matches, and the -h options print sizes in readable units.

Troubleshoot an Alias

  • Run type ll to see whether a name is an alias, a function, or a program.
  • Reload the file with source ~/.bashrc after every edit.
  • Avoid spaces around the equals sign. Write alias c='clear'.
  • Aliases do not work inside scripts by default. Use functions in scripts.

Key Takeaways

  • alias name='command' creates a shortcut.
  • Terminal aliases vanish on exit unless saved in ~/.bashrc.
  • source ~/.bashrc loads changes immediately.
  • Functions handle shortcuts that need arguments.
  • unalias removes a shortcut.