How to Add Customizations to .bashrc on Linux Mint

Share
In this tutorial, we'll learn how to customize an existing .bashrc file to add our own custom commands without modifying the existing code that is provided by default by Linux Mint.

Making a Backup

To begin with, let's make a backup copy of .bashrc so we can revert the changes in case we make a mistake.

Execute the following command in the terminal [how?]:

cp ~/.bashrc ~/.bashrc.original

This will create a copy of ~/.bashrc called .bashrc.original in your home directory.

Note: because both files start with a dot (they're dotfiles) they'll be hidden by default by file managers and by the ls command when listing files from the terminal.

To revert the changes, switch the arguments around. The cp command will overwrite the target if it exists.

cp ~/.bashrc.original ~/.bashrc

Opening .bashrc for Editing

Next, we'll need to open the .bashrc file in a source code editor to add our changes. There are a few ways we can do this. We could change our preferences in the file manager to display hidden files and then just double click on the file, or we could use the terminal.

If you have Linux Mint Cinnamon, it should have the xed text editor installed, which means we can execute the following command to open the file in it:

xed ~/.bashrc

Note: you don't need sudo in this case because the .bashrc file isn't a system file, it's a configuration file owned by the user.

The xed text editor should open and it should open the file automatically just as if we had double clicked on it.

Adding our Extension

Go to the end of the .bashrc file and add the following code:

# load my bashrc customization
if [ -f ~/.my-bashrc ]; then
    . ~/.my-bashrc
fi

The Bash script above checks if a file exists at the filepath ~/.my-bashrc, and if it does exist, executes it. This means that when .bashrc is executed, it will also execute .my-bashrc if the file exists.

This means we'll never need to edit .bashrc again. We can just edit .my-bashrc and leave .bashrc alone.

Save the .bashrc file and close xed.

Adding our Extension File

Now we just need to create our ~/.my-bashrc file and start adding our code to it. Let's try adding a simple test to see if it's working.

In the terminal, execute the following command:

xed ~/.my-bashrc

This will open the xed text editor with an empty file, but when we save, it will be saved to ~/.my-bashrc.

Inside the file, type the following code:

echo Hello world!

Save the ~/.my-bashrc file and open the terminal.

You should see the message "Hello world!" appear before the shell prompt is shown. If you execute a command, it won't appear again, meaning that echo is only executed when the terminal opens, and not after every single command.

Written by Noel Santos.

About the Author

I'm a self-taught Brazilian programmer graduated in IT from a FATEC. In a world of increasingly complex and essential computers, I decided to use my technical expertise in hardware, desktop applications, and web technologies to create an informative resource to make PC's easier to understand.

View Comments