Skip to content
Français
Account: not signed in, sync available My learning
Select theme
Français

By updated

Improve this page

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 .bashrc and .bash_profile
  • Create aliases to shorten frequent commands
  • Customise the prompt PS1 with 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.

FileWhen it is readMain use
~/.bash_profileLogin (login shell: SSH, console)Environment variables, PATH
~/.bashrcEach new interactive terminalAliases, prompt, functions
~/.profileLogin (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.

Terminal window
cat ~/.bashrc

You 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:

Terminal window
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.

Terminal window
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:

  1. Open the file with an editor:

    Terminal window
    nano ~/.bashrc
  2. Add your aliases at the end of the file:

    Terminal window
    # My personal aliases
    alias ll='ls -la --color=auto'
    alias ..='cd ..'
  3. Save and exit (Ctrl+O, Enter, Ctrl+X in nano).

  4. Apply the changes:

    Terminal window
    source ~/.bashrc

List and remove an alias

To see every active alias, type alias with no argument:

Terminal window
alias

To remove an alias temporarily, use unalias:

Terminal window
unalias ll

To 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:

Terminal window
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.

CodeShows
\uUser name
\hMachine name (short)
\wCurrent directory (full path)
\WCurrent directory (name only)
\dDate
\tTime (HH:MM:SS)
\$$ (regular user) or # (root)

Examples

A simple prompt with the user, the machine and the directory:

Terminal window
PS1='\u@\h:\w\$ '
Output
bob@server:~/projects$

A minimalist prompt with only the current folder:

Terminal window
PS1='\W \$ '
Output
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.

Terminal window
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.

CodeColour
32Green
34Blue
31Red
33Yellow
0Reset

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:

Terminal window
source ~/.bashrc

If 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:

Terminal window
# Add ~/scripts to the PATH
export PATH="$HOME/scripts:$PATH"
# Default editor
export EDITOR="nano"

After the change, do not forget to reload the file in the current shell:

Terminal window
source ~/.bashrc

What does the source command do?

source rereads a file and applies its commands in the current shell:

Terminal window
source ~/.bashrc

It is the standard way to reload your configuration without closing the terminal. The equivalent shortcut is . (a dot):

Terminal window
. ~/.bashrc

Troubleshooting

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.

SymptomLikely causeFix
Alias not recognised after a restartAlias not added to ~/.bashrcAdd the line to the file
source ~/.bashrc shows an errorSyntax error in the fileCheck the line given in the message
Odd prompt after changing PS1Colour codes badly closed (\[...\] missing)Check the \[ and \] markers
Variable lost over SSHVariable in .bashrc but the login session reads .bash_profileAdd source ~/.bashrc to .bash_profile
nano ~/.bashrc creates a new empty fileThe file does not exist (rare on recent distributions)Normal, the file will be created

Key points

  • ~/.bashrc is the main customisation file for interactive terminals.
  • An alias (alias name='command') creates a shortcut to a long command.
  • PS1 controls the look of the prompt; the \u, \h, \w codes insert useful information.
  • source ~/.bashrc applies the changes without closing the terminal.
  • Add your exports to ~/.bashrc to 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

6 questions
6 min
70% to pass

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

Is this project useful to you?

The guides, courses, quizzes and labs stay free. If you use them regularly, you can contribute to their upkeep and development.

Become a supporting member (new tab) One-time support (new tab) Learn more

Follow the project on LinkedIn