What is Linux bashrc and How to Use It [Full Guide]

Image result for linux bashrc wallpaper

If you are a Linux user and are learning about its command line, you might know by now that BASh is a Linux shell and stands for “Bourne Again Shell”. You are also likely to have BASh installed as your default terminal. This is because it is both the most common and, likely, the most popular of Linux shells. It basically interprets your typed input in the Terminal program and runs commands based on your input. Unlike some other terminal customization tricks, playing around with .bashrc is fairly straight-forward and low risk. If you mess anything up, you can always delete the .bashrc file completely and start over again. In this article, we will explore what BASh is and what .bashrc files are, and how to use them.

What Is a Shell?

A shell can be described as an interpreter that can accept commands from the user and run them to perform operations such as navigating around a file system, running programs, and interacting with devices. There are a number of different shells including csh, zsh, dash, and korn. As was mentioned, BASh is the most common of shells for Linux as it also allows some degree of customization through scripting, which leads us to “.bashrc”

What Are Bashrc Files and How to Use Them?

.bashrc is a shell script that Bash runs whenever it is started interactively. It initializes an interactive shell session. Any commands that you could type at the command prompt, You can put in that file. It is executed whenever a new terminal session is started in interactive mode. This is what happens when you open a new terminal window by pressing Ctrl+Alt+T, or just open a new terminal tab. In order to load your preferences, bash runs the contents of the bashrc file at each launch. This shell script is found in each user’s home directory. It’s used to save and load your terminal preferences and environmental variables.

So the first step is to open a new terminal window (Ctrl+Alt+T).

The computer returns three results upon running this command:

  • /etc/skel/.bashrc
  • /home/gary/.bashrc
  • /root/.bashrc

/etc/skel/.bashrc file is copied into the home folder of any new users that are created on a system.

/home/ali/.bashrc is the file used whenever the user Ali opens a shell and the root file is used whenever root opens a shell.

What Can You Do With .bashrc Files?

There’s a couple of useful hacks that you can use to make your terminal experience more efficient and user-friendly. We will explore them here.

Bash Prompts

A customized bash prompt makes your work on the terminal more productive and efficient as it allows you to personalize your terminal and have it to show prompts when you run a command. For example, you can:

Change color on bad command

This prompt changes the color if your last command failed to run successfully, but it also shortens long paths and contains the bash history number of each command for easy retrieval. This can be very helpful and efficient.


PROMPT_COMMAND='PS1="\[\033[0;33m\][\!]\'if [[ \$? = "0" ]];
then echo "\\[\\033[32m\\]"; else echo "\\[\\033[31m\\]";
fi\'[\u.\h: \'if [[ \pwd|WC -c|tr -d " "' > 18 ]]; then echo
"\\W"; else echo "\\w"; fi\']\$\[\033[0m\] "; echo -ne
"\033]0; hostname -s':'pwed'\007"'

Multiline prompt


This is a multi-line prompt containing date/time, full path, user and host, active terminal, even file count, and space usage. This can be useful for those who would like a lot of information in their prompt.


PS1="\n\[\033[35m\]\$(/bin/date)\n\[\033[32m\]\w\n\[\033[1;31m\]\[email protected]\h: \[\033[1;34m\]\$(/usr/bin/tty | /bin/sed -e 's:/dev/::'):
\[\033[1;36m\]\$(/bin/ls -1 | /usr/bin/wc -l | /bin/sed 's:
::g') files \[\033[1;33m\]\$(/bin/ls -lah | /bin/grep -m 1 total
| /bin/sed 's/total //')b\[\033[0m\] -> \[\033[0m\]"

Display directory information


With this prompt, there are user/host, number of jobs, and date/time on the top line. Below that is the current directory along with the number of files in that directory and their disk usage.


PS1="\n\[\e[30;1m\]\[\016\]l\[\017\](\[\e[34;1m\]\[email protected]\h\[\e[30;1m\])
(\[\[\e[32;1m\]\w\[\e[30;1m\])-9\[\e[32;1m\[\$(/bin/ls -1 |
/usr/bin/wc -l | /bin/sed 's: ::g') files, \$(/bin/ls -lah |
/bin/grep -m 1 total | /bin/sed 's/total //')b\[\e[30;1m\])--> \[\e[0m\]"

High performance simple prompt


If you want something that packs a lot of info without slowing down your computer, you can use this prompt.


PS1="[\d | \T -> \w ...\$?]\n#"

The Rob prompt


This is a simple and small code, but it allows the features found in the larger, more complex examples.


PS1="\[\033[0;33m\][\!]\ \if [[ \$? = "0" ]]; then echo
"\\[\\033[32m\\]"; else echo "\\[\\033[31m\\]"; fi\'[\u.\h: \'
if [[ 'pwed|wc -c|tr -d " "' > 18 ]]; then echo "\\W"; else echo
"\\w"; fi\']\$\[\033[0m\] "; echo -ne "\033]0;'hostname -s':'pwed'\007"

Note – to use any of these prompts, you can copy & paste the “PS1=” line directly into your terminal. To make the change permanent, paste the line to the end of your ~/.bashrc file.


You can save yourself some time by creating aliases for your most-used commands. Aliases are like custom shortcuts used to represent a command (or set of commands) executed with or without custom options. For example, the command “ls”;

By default, “ls” displays the contents of your directory. That’s useful, but it’s often more useful to know more about the directory, or know the hidden contents of the directory. Therefore, we use an alias here. A common alias is “ll”, which is set to run “ls –lha” or something similar. That will display the most details about files, revealing hidden files, and showing file sizes in units readable to us instead of blocks.

You can use this to create shorter versions of commands, guard against common typos, or force a command to always run with your favored flags. You can also circumvent annoying or easy-to-forget syntax with your own preferred shorthand.

You can see a list of defined aliases on your profile by simply executing the alias command: $ alias

Now you are shown the default aliases defined for your user in Ubuntu 18.04

As you can see, $ ll Is equivalent to running $ ls -alF

You can also create your own temporary aliases.

Type the word alias then use the name you wish to use to execute a command, followed by “=” sign and quote the command you wish to alias.

The syntax is as follows:

$ alias shortName="your custom command here"

For example:

$ alias wr="cd /var/www/html"

You can then use the “wr” shortcut to go to the webroot directory.

However, the problem with that alias is that it will only be available for your current terminal session. If you open a new terminal session, the alias will no longer be available. If you wish to save your aliases across sessions, you will need a permanent alias:

To keep aliases between sessions, you can save them in your user’s bashrc file.


The syntax you should use is practically the same as creating a temporary alias, except this time you also have to save it in a file. So:

Step 1: for example, in bash, open a .bashrc file like this:

$ vim ~/.bashrc

Step 2: Find a place in the file, where you want to keep the aliases. For example, you can add them at the end of the file.

Step 3: Save the file. The file will be automatically loaded in your next session.

Note: To remove an alias use the “unalias” command.

$ unalias alias_name
$ unalias -a [remove all alias]

Editing .bashrc files

You can edit bashrc to add your own commands in any terminal text editor. We will use nano editor in the following examples.

Step 1: To edit bashrc using nano, put the following command in Terminal:

nano ~/.bashrc

Note: If it is the first time you are editing your .bashrc file, you might find that it’s empty. That is not a problem. If not empty, you can feel free to put your additions on any line.

Bear in mind that any changes you make to .bashrc will be applied next time you launch the terminal. If you want to apply them immediately, run the command below:

source ~/.bashrc

Step 2: You can add to your .bashrc whereever you like, but use a command (proceeded by #) to organize your code.

Note: Bear in mind that edits in .bashrc have to follow bash’s scripting format. If you don’t know how to script with bash, there are a number of resources you can use online.



Apart from single shorthand commands, you can also combine multiple commands into a single operation using BASh functions. Sometimes they get complicated and confusing, but they generally follow this syntax:

function_name () {

The command below combines “mkdir” and “cd” commands. Typing “md folder_name” creates a directory named “folder_name” in your working directory and navigates into it immediately.

md () {
        mkdir -p $1
        cd $1

The “$1” you see in the function represents the first argument, which is the text you type immediately after the function name.

In conclusion, the .bashrc file is a very powerful tool and is a great way to customize your Linux shell. If you get the hang of it and are able to use it in the correct way, you will increase your productivity and efficiency by lots!

3.7 6 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Money-Back Guarantee Close Button
Would love your thoughts, please comment.x