LearnWithShobhan

Customize your shell: .bashrc vs .zshrc, aliases and the PS1 prompt

Shobhan Biswas··4 min read
>_From the course CLI Mastery: 57 lessons, lifetime access.

You create a handy shortcut, close the terminal, open a new one, and the shortcut is gone. The same happens with environment variables you set by hand. The fix is a small hidden file that your shell reads every time it starts. Once you know it, you can shape your shell into something that feels like yours.

Why your changes disappear

When you run alias or export in a terminal, you change the current session only. A session starts when you open a terminal window and ends when you close it. Everything you set inside it lives in memory and is lost at the end.

To keep a setting, you need it to be recreated automatically at the start of every session. That is exactly what the shell's configuration files are for.

Which file does your shell read?

Whenever a new session starts, the shell first runs one or two startup scripts. Whatever is in them gets loaded: aliases, environment variables, the prompt, even a welcome message.

ShellMain file you editAlso read at startup
Bash~/.bashrc~/.bash_profile or ~/.profile (login shells)
Zsh (macOS default)~/.zshrc~/.zprofile

Not sure which shell you are in? Print its name:

bash
$ echo $0
-zsh

These files sit in your home folder (~) and their names start with a dot, which hides them from a plain ls. Add -a to show everything:

cd ~
ls -la

Look for .zshrc or .bashrc in the list. If it does not exist yet, you can create it.

Edit the file and prove it works

Open it in any editor. Nano is the friendliest one to start with:

nano ~/.zshrc

Add a single line at the end, save, and close the terminal:

echo "Welcome back"

Open a new terminal window. You'll see Welcome back printed as the session starts. Nothing else happened: the shell simply executed the file. The same thing is behind the "last login" and other messages you may see at startup.

Aliases: shortcuts for long commands

An alias gives a short name to a longer command. Add these to your config file:

alias ld="ls -ld"
alias ll="ls -lah"

ll is a very common one: ls -lah lists everything (-a), in long format (-l), with human-readable sizes (-h). Now ll does all that.

Environment variables, permanently

You can do the same with variables. This is where the line from the PATH article goes if you want it to last:

export PATH="$HOME/bin:$PATH"

New to PATH? Read How the shell finds commands first.

Load changes without reopening the terminal

After editing the file, your current session does not know about the change. You have two options:

  1. Close the terminal and open a new one.
  2. Load the file into the current session with source:
source ~/.zshrc

If you try the new alias before doing either, you'll get a "command not found" error, because the shell hasn't read the file again yet.

Customize the prompt: PS1 and PROMPT

The text before your cursor, usually your folder name and a $ or %, is the primary prompt. It is stored in a variable:

  • In Bash the variable is PS1.
  • In Zsh the variable is PROMPT (Zsh also understands PS1).

Try a simple one in your config file, then source it:

# Bash: user@host:folder$
PS1='\u@\h:\w\$ '
 
# Zsh: user@host:folder %
PROMPT='%n@%m:%~ %# '

The cryptic characters are placeholders: in Bash \u is your username, \h the host, \w the current folder; in Zsh %n, %m and %~ do the same. You don't need to memorize them. Search for a Bash prompt generator or a Zsh prompt generator, click together what you want to show (current folder, date, git branch), and paste the result into your config file.

Free to watch. Get all 57 lessons →

Key takeaways

  • ✓Aliases and variables set in a terminal vanish when it closes, because they only live in that session.
  • ✓The shell runs startup files every time: ~/.bashrc for Bash and ~/.zshrc for Zsh (plus a profile file).
  • ✓Put alias and export lines in the config file to keep them, and use source to load changes into the current session.
  • ✓The prompt is a variable: PS1 in Bash, PROMPT in Zsh. A prompt generator saves you from memorizing the placeholders.

Learn the terminal properly, once

CLI Mastery is 57 short lessons that take you from "what is a shell" to scripting and running a web server on AWS. Every idea is drawn first, then done live. Lifetime access, and three lessons are free to watch.

Free download

Linux command line cheat sheet

One printable page: navigation, files, permissions, pipes, processes, scripts and SSH.

Occasional emails with new tutorials and courses. Unsubscribe any time.

Keep reading