TExcelTAD
Home › Linux & Bash › Shell Configuration (.bashrc)

Shell Configuration (.bashrc)

Every time you open a terminal, Bash reads a few startup files from your home directory. Putting your aliases, functions, PATH changes and prompt settings there makes your shell feel like your own. Knowing which file is read when also explains many "it works in my terminal but not in my script" puzzles.

Which file runs when?

Shell type Example Reads
Login shell SSH login, a console login, bash -l /etc/profile, then the first of ~/.bash_profile, ~/.bash_login, ~/.profile
Interactive non-login A new terminal window/tab on most desktops ~/.bashrc
Non-interactive Running a script, bash -c, cron Nothing (only the file named in $BASH_ENV)

Because the two interactive kinds read different files, the standard setup is: put everything in ~/.bashrc, and make ~/.bash_profile load it:

bash
# ~/.bash_profile
[ -f ~/.bashrc ] && . ~/.bashrc

Scripts read none of these, which is why a script shouldn't rely on your aliases or on PATH changes made in .bashrc.

A typical ~/.bashrc

bash
# ~/.bashrc
# Stop here if the shell is not interactive
[[ $- != *i* ]] && return

export EDITOR=nano
export PATH="$HOME/.local/bin:$PATH"

# History: bigger, no duplicates, appended rather than overwritten
HISTSIZE=10000
HISTCONTROL=ignoreboth:erasedups
shopt -s histappend

# Quality of life
shopt -s globstar autocd
alias ll='ls -lh'
alias la='ls -A'
alias gs='git status'
mkcd() { mkdir -p -- "$1" && cd -- "$1"; }

PS1='\u@\h:\w\$ '

After editing, apply the changes to the current shell with source ~/.bashrc (or open a new terminal).

Aliases

An alias is a short name for a longer command. They're expanded only in interactive shells, unless a script turns them on with shopt -s expand_aliases, as this example does to demonstrate:

bash · try it yourself
shopt -s expand_aliases
alias ll='ls -1A'
alias count='wc -l'
touch notes.txt todo.txt
ll | grep -v gitconfig
printf 'a\nb\nc\n' | count
alias ll
unalias ll
type ll 2>&1
notes.txt
todo.txt
3
alias ll='ls -1A'
/w/main.sh: line 9: type: ll: not found
Command Effect
alias List all aliases
alias name='cmd' Define one (no spaces around =)
unalias name Remove it
\ls or command ls Run the real command, bypassing an alias

For anything with arguments in the middle or logic, use a function instead: aliases only substitute text at the start of a command.

Loading a configuration file

The same mechanism that loads .bashrc, source, works for any file. Here a small rc file is written and sourced, and its function and variables become available:

bash · try it yourself
cat > ~/.bashrc <<'RC'
export PROJECTS="$HOME/projects"
mkcd() { mkdir -p -- "$1" && cd -- "$1"; }
greet() { echo "Welcome back, $(whoami). Projects live in $PROJECTS"; }
RC
source ~/.bashrc
greet
mkcd "$PROJECTS/new-app"
pwd
Welcome back, learner. Projects live in /home/learner/projects
/home/learner/projects/new-app

mkcd has to be a function: a script couldn't change the directory of your shell, but a function runs inside it.

The prompt: PS1

PS1 defines the prompt using backslash escapes:

Escape Shows
\u User name
\h Host name (up to the first dot)
\w Current directory, with ~ for home
\W Only the last part of the directory
\$ $ for normal users, # for root
\t Time (HH:MM:SS)
\[\e[32m\] … \[\e[0m\] Colours (the \[ \] tell Bash the codes take no space)

${PS1@P} expands a prompt string exactly as Bash would display it, so you can preview designs:

bash · try it yourself
mkdir -p ~/projects/site
cd ~/projects/site
for PS1 in '\u@\h:\w\$ ' '[\W]\$ ' '\h \w > '; do
  printf '%-14s -> %s\n' "$PS1" "${PS1@P}"
done
\u@\h:\w\$     -> learner@tad:~/projects/site$
[\W]\$         -> [site]$
\h \w >        -> tad ~/projects/site >

Shell options worth knowing

shopt -s NAME enables and shopt -u NAME disables an option; set -o NAME covers another group:

Option Effect
shopt -s globstar ** matches recursively
shopt -s autocd Type a directory name to cd into it
shopt -s cdspell Fix small typos in cd arguments
shopt -s histappend Append to the history file instead of overwriting
set -o vi vi-style line editing (default is emacs-style)
set -o noclobber > won't overwrite existing files
bash · try it yourself
shopt globstar
shopt -s globstar
shopt globstar
set -o noclobber
echo "first" > note.txt
echo "second" > note.txt
echo "forced" >| note.txt
cat note.txt
globstar            	off
globstar            	on
/w/main.sh: line 6: note.txt: cannot overwrite existing file
forced

History tips

Keys / command Effect
Ctrl+R Search backwards through history
history 20 Show the last 20 commands
!! Repeat the last command (sudo !! is a classic)
!$ The last argument of the previous command
HISTIGNORE='ls:cd' Don't record these commands

Tip: Keep your dotfiles (.bashrc, .gitconfig, .vimrc, …) in a Git repository. Setting up a new machine then takes a minute.

Test yourself

Which file does Bash read when you open a new (non-login) terminal window?

  1. ~/.profile
  2. ~/.bashrc
  3. /etc/passwd
  4. ~/.bash_history

You added an alias to ~/.bashrc. How do you use it in the current terminal without reopening it?

  1. bash ~/.bashrc
  2. source ~/.bashrc
  3. export ~/.bashrc
  4. chmod +x ~/.bashrc

Why can't a script use the aliases from your .bashrc?

  1. Aliases are only for root
  2. Non-interactive shells don't read .bashrc and don't expand aliases
  3. Scripts can't run ls
  4. Aliases expire