ZetCode

Bash Aliases and the .bashrc File

last modified October 24, 2026

An alias is a short name for a longer command. Bash replaces the alias with its text when it reads a command line, which saves typing for commands used every day. Aliases, shell functions, the prompt, and other settings are made permanent in startup files such as ~/.bashrc, which Bash reads when it starts.

The examples were run with Bash 5.2 on Debian 12, as the user jano with the default startup files that Debian copies from /etc/skel. Interactive commands were typed into an interactive shell; the prompt is shortened to $.

Listing aliases

The alias command without arguments lists the defined aliases.

alias
type ls
ls project

Debian's default ~/.bashrc defines one alias, which makes ls use colours in a terminal. The type command tells how Bash interprets a name: as an alias, a function, a built-in command, or a program in the PATH.

$ alias
alias ls='ls --color=auto'
$ type ls
ls is aliased to `ls --color=auto'
$ ls project
README.md  src

Defining an alias

An alias is defined with alias name='text'. There must be no spaces around the =.

alias ll='ls -alF'
ll project
alias ll
type ll

ll now runs ls -alF: a long listing of all files, with a / after directory names. alias NAME prints a single definition. An alias defined on the command line lasts until the shell exits.

$ alias ll='ls -alF'
$ ll project
total 16
drwxr-xr-x 3 jano jano 4096 Oct 24 19:50 ./
drwxrwxr-x 3 jano jano 4096 Oct 24 19:50 ../
-rw-r--r-- 1 jano jano   10 Oct 20 10:00 README.md
drwxr-xr-x 2 jano jano 4096 Oct 24 19:50 src/
$ alias ll
alias ll='ls -alF'
$ type ll
ll is aliased to `ls -alF'

How aliases are expanded

Bash replaces only the first word of a command. Arguments typed after an alias are appended to its text.

alias ll='ls -alF'
ll project/src
echo ll
sudo ll project
alias sudo='sudo '
sudo ll project

ll project/src becomes ls -alF project/src. In echo ll, ll is an argument and stays as it is. For the same reason, sudo ll fails: sudo looks for a program called ll. When the text of an alias ends with a space, Bash also checks the next word for an alias, so the alias sudo='sudo ' makes sudo ll work.

$ alias ll='ls -alF'
$ ll project/src
total 12
drwxr-xr-x 2 jano jano 4096 Oct 24 19:50 ./
drwxr-xr-x 3 jano jano 4096 Oct 24 19:50 ../
-rw-r--r-- 1 jano jano   12 Oct 24 19:50 app.py
$ echo ll
ll
$ sudo ll project
sudo: ll: command not found
$ alias sudo='sudo '
$ sudo ll project
total 16
drwxr-xr-x 3 jano jano 4096 Oct 24 19:50 ./
drwxrwxr-x 3 jano jano 4096 Oct 24 19:50 ../
-rw-r--r-- 1 jano jano   10 Oct 20 10:00 README.md
drwxr-xr-x 2 jano jano 4096 Oct 24 19:50 src/

Single and double quotes

The quotes in a definition decide when variables are expanded.

alias here1="echo $PWD"
alias here2='echo $PWD'
alias here1 here2
cd /tmp
here1
here2

With double quotes, $PWD was expanded when the alias was defined, so here1 always prints the old directory. With single quotes, the alias contains the text $PWD, which is expanded each time the alias runs. Use single quotes unless you want a value fixed at definition time.

$ alias here1="echo $PWD"
$ alias here2='echo $PWD'
$ alias here1 here2
alias here1='echo /home/jano/work'
alias here2='echo $PWD'
$ cd /tmp
$ here1
/home/jano/work
$ here2
/tmp

Bypassing and removing aliases

An alias can be bypassed for a single command, or removed with unalias.

alias ls='ls -F'
ls project
\ls project
command ls project
unalias ls
ls project
unalias ls

A backslash before the name, or the command built-in, runs the real command without alias expansion. unalias removes the alias for the rest of the session, and complains if the alias does not exist. unalias -a removes all aliases.

$ alias ls='ls -F'
$ ls project
README.md  src/
$ \ls project
README.md  src
$ command ls project
README.md  src
$ unalias ls
$ ls project
README.md  src
$ unalias ls
bash: unalias: ls: not found

Functions instead of aliases

Aliases cannot use arguments in the middle of their text. A shell function can.

alias mkcd='mkdir -p $1 && cd $1'
mkcd demo
mkcd() { mkdir -p -- "$1" && cd -- "$1"; }
unalias mkcd
mkcd() { mkdir -p -- "$1" && cd -- "$1"; }
mkcd demo/nested
pwd
type mkcd

The alias does not work: $1 is empty in an interactive shell, so mkcd demo becomes mkdir -p && cd demo. Defining a function of the same name fails, because Bash expands the alias mkcd in the definition itself; we must remove the alias first. Inside a function, $1 is the first argument. The -- ends the options, so a directory name that starts with a dash works too. A function runs in the current shell, so its cd changes the shell's directory.

$ alias mkcd='mkdir -p $1 && cd $1'
$ mkcd demo
mkdir: missing operand
Try 'mkdir --help' for more information.
$ mkcd() { mkdir -p -- "$1" && cd -- "$1"; }
bash: syntax error near unexpected token `('
$ unalias mkcd
$ mkcd() { mkdir -p -- "$1" && cd -- "$1"; }
$ mkcd demo/nested
$ pwd
/home/jano/work/demo/nested
$ type mkcd
mkcd is a function
mkcd ()
{
    mkdir -p -- "$1" && cd -- "$1"
}

Aliases in scripts

Aliases are meant for interactive use. Scripts do not see the aliases of the shell that starts them, and do not expand aliases at all by default.

uses-alias.sh
#!/bin/bash

alias ll='ls -alF'
ll project

Even the alias defined inside the script is not expanded, because alias expansion is off in non-interactive shells. It could be enabled with shopt -s expand_aliases, but scripts should use functions or full commands instead. bash -c starts a non-interactive shell, so it does not know ll either.

$ alias ll='ls -alF'
$ bash uses-alias.sh
uses-alias.sh: line 4: ll: command not found
$ bash -c 'll project'
bash: line 1: ll: command not found

Making aliases permanent

To keep aliases, put them in a startup file. Debian's ~/.bashrc reads ~/.bash_aliases if it exists, which keeps the aliases in a file of their own.

grep -n -A2 'bash_aliases' ~/.bashrc
cat >> ~/.bash_aliases <<'END'
alias ll='ls -alF'
alias la='ls -A'
alias ..='cd ..'
alias ports='ss -tlnp'
END
ll project
source ~/.bash_aliases
ll project
bash -i -c 'alias'

The here-document appends four aliases to the file. A running shell does not notice changes to startup files; source (or .) reads the file into the current shell. Every new interactive shell reads the aliases automatically, as the last command shows. The quoted 'END' keeps the shell from expanding anything inside the here-document.

$ grep -n -A2 'bash_aliases' ~/.bashrc
97:# ~/.bash_aliases, instead of adding them here directly.
98-# See /usr/share/doc/bash-doc/examples in the bash-doc package.
99-
100:if [ -f ~/.bash_aliases ]; then
101:    . ~/.bash_aliases
102-fi
103-
$ cat >> ~/.bash_aliases <<'END'
> alias ll='ls -alF'
> alias la='ls -A'
> alias ..='cd ..'
> alias ports='ss -tlnp'
> END
$ ll project
bash: ll: command not found
$ source ~/.bash_aliases
$ ll project
total 16
drwxr-xr-x 3 jano jano 4096 Oct 24 19:50 ./
drwxrwxr-x 4 jano jano 4096 Oct 24 19:51 ../
-rw-r--r-- 1 jano jano   10 Oct 20 10:00 README.md
drwxr-xr-x 2 jano jano 4096 Oct 24 19:50 src/
$ bash -i -c 'alias'
alias ..='cd ..'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
alias ports='ss -tlnp'

Functions in .bashrc

Functions that should be available in every shell also go into ~/.bashrc.

cat >> ~/.bashrc <<'END'

# create a directory and change into it
mkcd() {
    mkdir -p -- "$1" && cd -- "$1"
}

# extract common archive types
extract() {
    case $1 in
        *.tar.gz | *.tgz) tar -xzf "$1" ;;
        *.tar.xz)         tar -xJf "$1" ;;
        *.zip)            unzip -q "$1" ;;
        *.gz)             gunzip "$1" ;;
        *)                echo "extract: unknown archive type: $1" >&2; return 1 ;;
    esac
}
END
bash -n ~/.bashrc && echo "syntax OK"
tar -czf project.tar.gz project
mkcd unpacked
extract ../project.tar.gz
ls -R
extract ../notes.txt
declare -F | tail -n 3

bash -n checks the syntax of a file without running it, which is worth doing after each edit. The remaining commands were typed in a new interactive shell. extract picks the right tool by the file name and returns 1 for unknown types; return sets the exit status of a function. declare -F lists the names of defined functions.

$ cat >> ~/.bashrc <<'END'
>
> # create a directory and change into it
> mkcd() {
>     mkdir -p -- "$1" && cd -- "$1"
> }
>
> # extract common archive types
> extract() {
>     case $1 in
>         *.tar.gz | *.tgz) tar -xzf "$1" ;;
>         *.tar.xz)         tar -xJf "$1" ;;
>         *.zip)            unzip -q "$1" ;;
>         *.gz)             gunzip "$1" ;;
>         *)                echo "extract: unknown archive type: $1" >&2; return 1 ;;
>     esac
> }
> END
$ bash -n ~/.bashrc && echo "syntax OK"
syntax OK
$ tar -czf project.tar.gz project
$ mkcd unpacked
$ extract ../project.tar.gz
$ ls -R
.:
project

./project:
README.md  src

./project/src:
app.py
$ extract ../notes.txt
extract: unknown archive type: ../notes.txt
$ declare -F | tail -n 3
declare -f extract
declare -f mkcd

Which startup files are read

Bash reads different files depending on how it starts. To see it, we add an echo to the first line of ~/.profile and ~/.bashrc.

head -n 1 ~/.profile ~/.bashrc
bash -c 'echo command done'
bash -l -c 'echo command done'
bash -i -c 'echo command done'
bash -l -i -c 'echo command done'

A non-interactive shell running a command or script reads no startup file. A login shell (-l, also the shell after logging in on a console or with SSH) reads ~/.profile, and Debian's ~/.profile reads ~/.bashrc. An interactive non-login shell, such as a new terminal window in a desktop, reads ~/.bashrc only. In the non-interactive login shell, ~/.bashrc printed its line, but stopped right after it: its first real command returns when the shell is not interactive. So aliases and prompt settings belong in ~/.bashrc, and environment variables in ~/.profile; see the environment variables article.

$ head -n 1 ~/.profile ~/.bashrc
==> /home/jano/.profile <==
echo "reading ~/.profile"

==> /home/jano/.bashrc <==
echo "reading ~/.bashrc"
$ bash -c 'echo command done'
command done
$ bash -l -c 'echo command done'
reading ~/.profile
reading ~/.bashrc
command done
$ bash -i -c 'echo command done'
reading ~/.bashrc
command done
$ bash -l -i -c 'echo command done'
reading ~/.profile
reading ~/.bashrc
command done

The .bash_profile trap

A login shell reads only the first of ~/.bash_profile, ~/.bash_login, and ~/.profile that exists. Creating a ~/.bash_profile, as many instructions on the internet suggest, silently disables ~/.profile.

echo 'echo "reading ~/.bash_profile"' > ~/.bash_profile
bash -l -i -c 'type ll'
echo '[ -f ~/.profile ] && . ~/.profile' >> ~/.bash_profile
bash -l -i -c 'type ll'

With the new file, the login shell no longer read ~/.profile, and therefore not ~/.bashrc and the aliases either. On a server, this shows up as "my aliases work in a terminal but not over SSH". The fix is to read ~/.profile from ~/.bash_profile, or not to create ~/.bash_profile at all.

$ echo 'echo "reading ~/.bash_profile"' > ~/.bash_profile
$ bash -l -i -c 'type ll'
reading ~/.bash_profile
bash: type: ll: not found
$ echo '[ -f ~/.profile ] && . ~/.profile' >> ~/.bash_profile
$ bash -l -i -c 'type ll'
reading ~/.bash_profile
reading ~/.profile
reading ~/.bashrc
ll is aliased to `ls -alF'

Errors in startup files

A syntax error in a startup file prints a message when each shell starts, and the rest of the file may be skipped.

echo "alias gs='git status" >> ~/.bash_aliases
bash -n ~/.bash_aliases
bash -i -c 'echo shell started'
sed -i '$d' ~/.bash_aliases
bash -n ~/.bash_aliases && echo "syntax OK"

The new alias lacks its closing quote. bash -n finds the error before it bothers us in every new terminal. sed -i '$d' deletes the last line of the file. When you edit startup files on a remote machine, keep a second session open until a new login works.

$ echo "alias gs='git status" >> ~/.bash_aliases
$ bash -n ~/.bash_aliases
/home/jano/.bash_aliases: line 5: unexpected EOF while looking for matching `''
$ bash -i -c 'echo shell started'
bash: /home/jano/.bash_aliases: line 5: unexpected EOF while looking for matching `''
shell started
$ sed -i '$d' ~/.bash_aliases
$ bash -n ~/.bash_aliases && echo "syntax OK"
syntax OK

Customizing the prompt

The prompt is the value of the PS1 variable, usually set in ~/.bashrc. Backslash sequences insert information.

PS1='\u@\h:\w\$ '
cd project/src
PS1='[\t \W]\$ '
cd ..
PS1='$ '

\u is the user name, \h the host name, \w the full working directory with ~ for home, \W only its last component, and \t the time. \$ becomes # for root and $ for other users. The first form is Debian's default prompt without colours.

$ PS1='\u@\h:\w\$ '
jano@box:~/work$ cd project/src
jano@box:~/work/project/src$ PS1='[\t \W]\$ '
[19:51:45 src]$ cd ..
[19:51:46 project]$ PS1='$ '

Common aliases

The following aliases are popular. Pick the ones you really use; a long list of aliases is hard to remember.

AliasPurpose
alias ll='ls -alF'Long listing with hidden files
alias ..='cd ..'Go up one directory
alias grep='grep --color=auto'Highlight matches
alias df='df -h'Readable sizes
alias ports='ss -tlnp'Listening TCP ports
alias sudo='sudo 'Expand aliases after sudo
alias rm='rm -i'Ask before deleting; see the note below

Safety aliases such as rm -i are debated: they build the habit of relying on a prompt that is missing on other machines and in scripts, and one quickly learns to answer y without reading. Using rm -I or checking with ls first are alternatives.

Best practices

Source

Bash manual: Aliases, Bash manual: Bash Startup Files, Bash manual: Controlling the Prompt

In this article, we have defined, used, bypassed, and removed aliases, replaced aliases with functions where arguments are needed, made them permanent in ~/.bash_aliases and ~/.bashrc, looked at which startup files Bash reads, and customized the prompt. See also the environment variables and exit codes articles.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Linux tutorials.