More Bash answers

by Danny
Published 2 min read

Aliases typed in a terminal vanish when it closes. Learn which startup file your shell reads, from .zshrc to .bashrc and .bash_profile, and when a function beats an alias.

The short answer

#

An alias you type at the prompt only lives in that one shell session. To keep it, add it to the startup file your shell reads: ~/.zshrc for zsh, which is the default on macOS, or ~/.bashrc for Bash on most Linux systems. Then reload the file or open a new terminal.


_10
echo "alias gs='git status'" >> ~/.zshrc
_10
source ~/.zshrc

Why it happens

#

Every new shell starts with a clean slate and builds its environment by reading startup files. Which files it reads depends on the shell and on how it was started:

  • A Bash login shell reads /etc/profile, then the first of ~/.bash_profile, ~/.bash_login or ~/.profile that exists. It does not read ~/.bashrc on its own.
  • A Bash interactive non-login shell, such as a new terminal tab on most Linux desktops, reads ~/.bashrc.
  • Zsh reads ~/.zshenv for every shell, ~/.zprofile for login shells and ~/.zshrc for every interactive shell.

SSH sessions are login shells, and the macOS Terminal app opens login shells by default. That is why an alias in ~/.bashrc can work in one place and be missing in another. macOS has used zsh as the default shell since macOS Catalina, so on a Mac the file you usually want is ~/.zshrc.

How to fix it

#

Check which shell you are using


_10
echo $SHELL # your login shell
_10
ps -p $$ -o comm= # the shell running right now

Zsh: put aliases and functions in .zshrc


_10
# ~/.zshrc
_10
alias ll='ls -lah'
_10
alias gs='git status'

Bash: use .bashrc and load it from .bash_profile

Keep your aliases in ~/.bashrc, then make login shells read it too, so the same set is available everywhere:


_10
# ~/.bash_profile
_10
if [ -f ~/.bashrc ]; then
_10
. ~/.bashrc
_10
fi

Many Linux distributions already include this in the default ~/.profile or ~/.bash_profile, so check before adding it twice.

Use a function when you need arguments

An alias is a plain text substitution at the start of a command. It cannot take an argument in the middle or run several steps with logic. A shell function can, and the syntax below works in both Bash and zsh:


_10
# make a directory and move into it
_10
mkcd() {
_10
mkdir -p "$1" && cd "$1"
_10
}

Put functions in the same startup file as your aliases.

Share one file between shells

If you switch between Bash and zsh, keep aliases in their own file and load it from both:


_10
# in ~/.bashrc and ~/.zshrc
_10
[ -f ~/.aliases ] && . ~/.aliases

Common pitfalls

#
  • Editing the file and expecting the current terminal to change. Run source ~/.zshrc (or the file you edited), or open a new tab.
  • Putting spaces around the equals sign. alias gs = 'git status' fails; it must be alias gs='git status'.
  • Using double quotes around an alias that contains a variable. Double quotes expand the variable once, when the file loads; single quotes expand it each time the alias runs.
  • Expecting aliases inside scripts. Non-interactive Bash shells do not read ~/.bashrc and do not expand aliases, so use a function, or put a script in a directory on your PATH.
  • Shadowing a real command, such as alias rm='rm -i', and forgetting. Run type rm to see what a name resolves to, and command rm to skip the alias.
  • Adding the same line many times with >>. Open the file in an editor now and then to tidy it.
#

Was this resource helpful?