---
title: "How do I make an alias or function permanent in my shell?"
description: "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."
url: https://www.danny.engineering/answer/bash/how-do-i-make-an-alias-or-function-permanent-in-my-shell
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:23:00.000Z
tags: ["Bash"]
---

# How do I make an alias or function permanent in my shell?

## 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.


```bash
echo "alias gs='git status'" >> ~/.zshrc
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


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


### Zsh: put aliases and functions in .zshrc


```bash
# ~/.zshrc
alias ll='ls -lah'
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:


```bash
# ~/.bash_profile
if [ -f ~/.bashrc ]; then
  . ~/.bashrc
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:


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


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:


```bash
# in ~/.bashrc and ~/.zshrc
[ -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.

## Related reading

- [GNU Bash manual: Bash Startup Files](https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html)
- [GNU Bash manual: Aliases](https://www.gnu.org/software/bash/manual/html_node/Aliases.html)
