~/.bash_profile | Explained

Linux comes with a secure environment for the user to perform various tasks through the terminal. But what if the user wants to customize his bash environment? So if you are one of those looking for the answer, then you need to learn the ~/.bash_profile file in Linux, which we have discussed in detail in this article.

What is the Hierarchy of Startup Files in Linux?

In Linux, the startup hierarchy is controlled by four files at the login time. These are the “/etc/profile”,”/.bash_profile”, ”/etc/login”, and “/.profile” files. The following is a graphical representation of this hierarchy.

  • /etc/profile” is the file that contains all the user’s profile data in the system.
  • ./bash_profile” is the file used to modify the user configuration. It includes export variables and login commands.
  • /etc/login” manages the configuration of the password.
  • /.profile” contains the information of each user, which overrides the set variables.

When a user logs in, it first invokes the “/etc/profile” file and checks for the user. If the user is available, the information is given to the “/.bash_profile” file, configuring the user’s environment (for bash shell). “/etc/login” is the file that applies the particular configuration for the password. The “/.profile” file is available in the home directory, which the user can use to customize his environment.

What is the ~/.bash_profile Command in Linux?

~/.bash_profile is the hidden file the user creates for the configuration of the user environment. In most Linux Distributions, “/.profile” for the environment configuration is located in the home directory but hidden. To see the hidden files use the given command:

$ ls -a

Furthermore, you can view the “.profile” file by using the cat command:

$ cat .profile

The above image shows the content of the “.profile” file.

Let’s check the usage of /.bash_profile in Linux.

How to Use ~/.bash_profile in Linux?

In Linux, we can use “.bash_profile” by creating it. To create it, use the touch command in the terminal:

$ touch .bash_profile

The file is created successfully. Here, “.” shows that the file created is hidden. Users can view the hidden files using the “ls” command with the “a” option:

$ ls -a

The above image shows the “.bash_profile” is present in our home directory.

To check the execution of “.bash_profile”, let’s print the message through the “echo” command by opening it in the nano editor. To do so, execute the following command:

$ nano .bash_profile

After adding the echo line, save the file by pressing “Ctrl+O” and exit from the editor using “Ctrl+X”.

Now, run the source command to check the execution of the file:

$ source .bash_profile

The above image shows the successful execution of the echo command in “.bash_profile”.

Let’s move further and set the user bash environment.

How to Use the ~/.bash_profie With .bashrc File?

Using “.bash_profile”, the user can create his environment by linking it with the “.bashrc” file already in our Linux home directory. “.bashrc” is a file that stores the configuration of the terminal session, which executes first when the user login.

This file can be viewed using the “ls -a” command as shown in the below image:

$ ls -a

The user can also view the content of the “.bashrc” file using the “cat” command:

$ cat .bashrc

Open the “.bashrc” file in the nano editor and add the echo statement as shown:

$ nano .bashrc

After that, save the file and exit from the editor.

Now, open the .bash_profile file and add the following script to call the “.bashrc” file:

if [ -f ~/.bashrc ] then;
 .~/.bashrc
fi

Here, the “-f” option indicates the existence of the “.bashrc” file:

After adding the script, save the file and exit.

Now, reopen the terminal; you will see the echo line in the terminal. Whenever a user opens the terminal, the echo line will be printed on the screen as shown in the below image:

The above image shows that our “.bash_profile” is successfully linked with the “.bashrc” file.

Conclusion

In Linux, “.bash_profile” is the hidden file created in the home directory. This file is used to customize the user configuration environment. This post has briefly demonstrated the usage of the “.bash_profile” file. Besides this, creating the environment by linking it with the “.bashrc” file has also been illustrated in this post.