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.
_10echo "alias gs='git status'" >> ~/.zshrc_10source ~/.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_loginor~/.profilethat exists. It does not read~/.bashrcon its own. - A Bash interactive non-login shell, such as a new terminal tab on most Linux desktops, reads
~/.bashrc. - Zsh reads
~/.zshenvfor every shell,~/.zprofilefor login shells and~/.zshrcfor 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
_10echo $SHELL # your login shell_10ps -p $$ -o comm= # the shell running right now
Zsh: put aliases and functions in .zshrc
_10# ~/.zshrc_10alias ll='ls -lah'_10alias 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_10if [ -f ~/.bashrc ]; then_10 . ~/.bashrc_10fi
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_10mkcd() {_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 bealias 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
~/.bashrcand do not expand aliases, so use a function, or put a script in a directory on yourPATH. - Shadowing a real command, such as
alias rm='rm -i', and forgetting. Runtype rmto see what a name resolves to, andcommand rmto skip the alias. - Adding the same line many times with
>>. Open the file in an editor now and then to tidy it.
Related reading
#Was this resource helpful?