How to Reload .bash_profile in Linux Command-Line

The Linux operating system environment is famed for numerous OS-based attributes. One of them is the Linux home directory. It is responsible for all user profiles in the system and enables the system users to create and store files or access already existing/pre-defined system tools and resources.

The .bash_profile File

Since the .bash_profile file is defined in the Linux home directory, the system bash shell is dependent on it and other system files like /etc/profile, .bash_login, .bash_history, and .bash_logout to self-initialize.

For custom configuration of all Linux user environments, you need to be able to access and edit the .bash_profile configuration file. You can either change this file’s default settings or add new ones.

The .bash_profile file serves two primary objectives:

  1. Modification of the Linux operating system working environment by configuring and editing terminal settings, and custom environment variables.
  2. Provision of applications initialization instructions to the operating system.

Creating the .bash_profile File

First, create a .bash_profile file in your home directory if it doesn’t exist and open it with your favorite editor.

$ touch .bash_profile 
$ nano .bash_profile

Once the file has been created, we need to check for the existence of this created file. Since we created it with the '.' prefix, it will be hidden. Exit the nano editor (Ctrl+x) and run the following command:

$ ls -la
List Hidden Files in Linux
List Hidden Files in Linux

As you can see, several other bash files exist alongside it.

Edit the .bash_profile File

Use the nano editor to re-open this file so that we can start making some edits.

$ nano .bash_profile

We will populate our newly created .bash_profile file with some code. We will use the echo command to populate this file with a one-line code to make this tutorial interesting.

$ echo 'LinuxShellTips tutorial on reloading the .bash_pofile file'

Save this file to implement the changes made to it and exit the nano editor.

To activate the changes made, we will need to reload the .bash_profile file from the Linux terminal. Reloading this file requires the use of the “source” command.

$ source .bash_profile
or
$ source ~/.bash_profile

The latter command gives you the flexibility to reload the

file if you are on a different OS environment path and not directly on the Linux home directory.

Reload .bash_profile File
Reload .bash_profile File

Customizing the .bash_profile File

Let us open and modify the .bashrc file we viewed from the ls -la command.

$ nano .bashrc

At the bottom of this file, add an echo statement with the following details:

echo "This is a bashrc file"
Edit .bashrc File
Edit .bashrc File

We now want to link this .bashrc file to the .bash_profile file we created and modified earlier. Re-open the .bash_profile file.

$ nano .bash_profile

At the bottom of the file, add the following code.

# Getting aliases and functions 
if [-f ~/.bashrc]; then
         . ~/.bashrc
fi

The above code on the .bash_profile file will check for the existence of the .bashrc file and then try to reload it. Let us reload the .bash_profile file and see the outcome.

$ source ~/.bash_profile
Reload .bash_profile File
Reload .bash_profile File

We have successfully linked the .bash_profile and .bashrc files and executed .bashrc from .bash_profile. The phrase “This is a bashrc file” will be appearing each time you open a new terminal window.

Open the .bash_profile again.

$ nano .bash_profile

Let us set and export a system path variable with the following code:

PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PATH

Save the file and exit the nano editor.

Now, echo the $PATH variable from the command line.

$ echo $PATH
Check Environment Variables
Check Environment Variables

We have successfully set a system path variable and accessed it from the echo command.

Mastering the use of the .bash_profile file can give a system user flexibility like composing a welcome terminal message (This is a bashrc file) and even associating variable paths ($PATH) with the system user directories. There is more to it if you dig deeper.

Got something to say? Join the discussion.