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.
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 the most common failure when you customise your shell: a setting that works in the graphical terminal but 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 |
In practice, the question is settled in one sentence: put your customisations
in ~/.bashrc. Most distributions already set up .bash_profile to
load .bashrc at login, so a setting written in that single file is
active everywhere, in the console as over SSH. The reverse is not true.
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 aliases that cover most of the repetitive typing of an
administration day. Note the last one: redefining grep with an alias of the
same name is common practice, 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, then reload the file:
-
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:
Terminal window source ~/.bashrc
List and remove an alias
To see every active alias, type alias with no argument:
aliasTo remove an alias temporarily, use unalias:
unalias llTo remove it for good, delete the line from ~/.bashrc and run
source ~/.bashrc again.
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 any 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, which is the most effective visual signal
to avoid a destructive command run by mistake.
| Code | Shows |
|---|---|
\u | User name |
\h | Machine name (short) |
\w | Current directory (full path) |
\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 only lists the four colours that read best on a dark
background as on a light one. 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, it is almost always because a later
definition of PS1 exists 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:
# Add ~/scripts to the PATHexport PATH="$HOME/scripts:$PATH"
# Default editorexport EDITOR="nano"After the change, do not forget to reload the file in the current shell:
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):
. ~/.bashrcTroubleshooting
The five symptoms below cover almost every problem on this subject. Three of
them share the same root: a change made in the current terminal rather
than in the file, or the other way round. Before looking further, always
check these 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 session reads .bash_profile | Add source ~/.bashrc to .bash_profile |
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)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