Every time you open a terminal, Bash reads a configuration file to load
your settings. By adding a few lines to that file, you can create shortcuts,
change the look of the prompt and add useful variables. This guide shows you
how to adapt your working environment safely. Functions and tools such as fzf
and bash-it belong to the guide
Customise Bash, which comes at
the next level.
What you will learn
- Understand the difference between
.bashrcand.bash_profile - Create aliases to shorten frequent commands
- Customise the prompt
PS1with useful information - Apply the changes without closing the terminal
- Add permanent exports
Which configuration files does Bash read?
Bash reads different files depending on the type of session. This distinction explains why a setting that works in the graphical terminal vanishes over SSH, or the other way round. Look at the middle column: it decides whether your change will be taken into account in the context where you work.
| File | When it is read | Main use |
|---|---|---|
~/.bash_profile | Login (login shell: SSH, console) | Environment variables, PATH |
~/.bashrc | Each new interactive terminal | Aliases, prompt, functions |
~/.profile | Login (if .bash_profile does not exist) | Alternative to .bash_profile |
The table does not list ~/.bash_login, the second file Bash tries at login:
Bash reads the first one found among ~/.bash_profile, ~/.bash_login and
~/.profile, and ignores the others.
In practice, the question is settled in one sentence: put your customisations
in ~/.bashrc. On Debian and Ubuntu, it is ~/.profile that loads
.bashrc at login (these distributions usually have no ~/.bash_profile); on
other distributions, it is .bash_profile that does it. If one of these files
loads .bashrc, a setting written in .bashrc is active everywhere, in the
console as over SSH. The reverse is not true. If you create a ~/.bash_profile
yourself, it hides ~/.profile: it must then contain source ~/.bashrc.
See the current contents
Before adding anything, read what is already there. On Debian and Ubuntu,
~/.bashrc is over a hundred lines long, often with an aliases section
commented out at the end of the file: uncommenting is sometimes enough to
get what you are looking for.
cat ~/.bashrcYou will probably find default configuration lines added by your distribution.
How do you create aliases?
An alias is a shortcut for a long command. For example, instead of
typing ls -la --color=auto, you can create an ll alias:
alias ll='ls -la --color=auto'Now type ll, and the shell runs the long command for you.
Useful common aliases
Here are five handy aliases for repetitive typing. Note the last one: you
can redefine grep with an alias of the same name, and the shell does not
loop forever, because it does not expand an alias inside its own
definition.
alias ll='ls -la --color=auto'alias la='ls -A'alias ..='cd ..'alias ...='cd ../..'alias grep='grep --color=auto'Make an alias permanent
An alias created in the terminal disappears when it closes. To keep it, add it
to ~/.bashrc:
-
Open the file with an editor:
Terminal window nano ~/.bashrc -
Add your aliases at the end of the file:
Terminal window # My personal aliasesalias ll='ls -la --color=auto'alias ..='cd ..' -
Save and exit (Ctrl+O, Enter, Ctrl+X in nano).
-
Apply the changes in the open terminal, without closing it (the
sourcecommand is detailed in the section "What does the source command do?"):Terminal window source ~/.bashrc
List and remove an alias
To see every active alias:
aliasTo remove an alias temporarily:
unalias llTo remove it for good, delete the line from ~/.bashrc: new terminals will no
longer know it. In the terminal that is already open, it stays active until you
run unalias, because source adds settings but never removes any.
How do you customise the PS1 prompt?
The PS1 variable controls the look of the prompt. To see its current value:
echo "$PS1"The basic codes
These seven sequences are enough to build a useful prompt. Two of them
deserve particular attention: \w replaces your home directory with a
tilde (~/projects and not /home/bob/projects), and \$ shows #
when the effective user is root. This change of character warns you before you
run a destructive command by mistake.
| Code | Shows |
|---|---|
\u | User name |
\h | Machine name (short) |
\w | Current directory (~ for the home directory) |
\W | Current directory (name only) |
\d | Date |
\t | Time (HH:MM:SS) |
\$ | $ (regular user) or # (root) |
Examples
A simple prompt with the user, the machine and the directory:
PS1='\u@\h:\w\$ 'bob@server:~/projects$A minimalist prompt with only the current folder:
PS1='\W \$ 'projects $Add colour
Colour codes are wrapped between \[\033[CODEm\] and \[\033[0m\]. The
\[ and \] delimiters are not decorative: they tell Bash that the
characters they surround take up no column on screen. Forgetting them
produces the symptom described in the troubleshooting table further down, a
prompt that erases itself or duplicates as soon as you recall a command with
the up arrow.
PS1='\[\033[32m\]\u@\h\[\033[0m\]:\[\033[34m\]\w\[\033[0m\]\$ 'The table below gives four colours and the reset code. Yellow reads badly on
a light background: choose according to your terminal. The 0 code is
essential at the end of the prompt: without it, the colour spills over
everything you type.
| Code | Colour |
|---|---|
32 | Green |
34 | Blue |
31 | Red |
33 | Yellow |
0 | Reset |
Make the prompt permanent
Add the PS1=... line to ~/.bashrc, at the end of the file so that it
overrides the one the distribution already set, then reload:
source ~/.bashrcIf the prompt does not change, first look for a later definition of PS1
further down the file. grep -n 'PS1=' ~/.bashrc lists every assignment with
its line number: the last one wins.
How do you add permanent exports?
For an environment variable to be always available, add an export to
~/.bashrc. The word export makes the variable visible to the programs you
start from the shell (git, crontab, a script); without it, it stays a plain
shell variable. The guide on
environment variables
details this difference.
# Add ~/scripts to the PATHexport PATH="$HOME/scripts:$PATH"
# Default editorexport EDITOR="nano"After the change, do not forget:
source ~/.bashrcWhat does the source command do?
source rereads a file and applies its commands in the current shell:
source ~/.bashrcIt is the standard way to reload your configuration without closing the
terminal. The equivalent shortcut is . (a dot):
. ~/.bashrcDo not run bash ~/.bashrc instead. That command starts a new shell, a
child process, which reads the file and then exits: the aliases and variables it
defined disappear with it, and your terminal sees nothing new. source runs the
file in the current shell, so its settings stay active.
Troubleshooting
Here are five symptoms you may meet while following this guide. The first comes
from a setting typed in the current terminal instead of written in the
file. Before looking further, always check two points: the file really
contains the line, and source ~/.bashrc really was run again after writing it.
| Symptom | Likely cause | Fix |
|---|---|---|
| Alias not recognised after a restart | Alias not added to ~/.bashrc | Add the line to the file |
source ~/.bashrc shows an error | Syntax error in the file | Check the line given in the message |
| Odd prompt after changing PS1 | Colour codes badly closed (\[...\] missing) | Check the \[ and \] markers |
| Variable lost over SSH | Variable in .bashrc, but the login file that is read (for example a ~/.bash_profile that only contains an export PATH) does not load .bashrc | Add source ~/.bashrc to that login file |
nano ~/.bashrc creates a new empty file | The file does not exist (rare on recent distributions) | Normal, the file will be created |
Key points
~/.bashrcis the main customisation file for interactive terminals.- An alias (
alias name='command') creates a shortcut to a long command. PS1controls the look of the prompt; the\u,\h,\wcodes insert useful information.source ~/.bashrcapplies the changes without closing the terminal.- Add your exports to
~/.bashrcto make them permanent.
Knowledge check
Check that the essentials of this guide stuck. The questions cover only what has just been explained here.
Knowledge check
Check what you have learned with this interactive quiz
How it works
- The timer starts when you click Start
- Multiple choice, true/false and short answer questions
- You can move between questions
- Detailed results are shown at the end
Starts the quiz and the timer
Review
(0/0)Result saved on this device. It will be sent to your account at the next sync. Sync now
Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
Continue the course: Your first Bash script: create and run it step by step
A new full quiz with random questions
Work again only on the questions you missed