What is the difference between ~/.profile and ~/.bash_profile?

In Linux, the startup files allow the users to customize the terminal and desktop to perform specific tasks every time the users start the system. The startup files, such as ~./profile and ~./profile contain the data for a particular user which avoids setting the environment variables and running the customized functions manually.

This article will explain the basic workings and differences between ~/.profile and ~/.bash_profile files with the following timeline:

  • What are Bash Start-up Files in Linux?
  • What are the ~/.profile and ~/.bash_profile Files in Linux?
  • Differences Between ~/.profile and ~/.bash_profile

What are Bash Start-up Files in Linux?

Several startup files run when the user logs in. These files run in a hierarchy which is as follows:

On startup, the /etc/profile (system-wise) executes, which loads all the user’s profile data present in the system. While the .profile (user-wise)  file contains only your data which overrides the environment variables set in the /etc/profile file. The .profile file is in the $HOME directory and allows the user to customize its variables by modifying them.

If the ~./bash_file exists in the system, it executes while logging in; else, it executes the next existing start-up file. The ~./bash_profile file is compatible with the bash shell, and if the bash shell is not the user’s default shell, it does not execute the “bash” start-up files.

The .profile file is present in all the shells, which sets the custom variables for the logged-in user profile only and contains the following system configurations:

  • To open desired shell
  • The terminal prompt (blinking mouse) appearance
  • Keyboard sound

What are the ~/.profile and ~/.bash_profile Files in Linux?

The .profile file contains specific start-up files based on a specific shell. For the bash shell, the .bash_profile is executed, while for all other shells (such as Ksh, Csh, Bourne, etc. ), the .profile configuration file is executed.

The .bash_profile file is a bash shell script config file stored at ~/.bash_profile and gets executed every time a user logs into a system. This .bash_profile file and .profile are invoked when you enter your username and password to log in on your Linux system, while the login can be to the local or remote server.

To access or add user custom functions present in the .bash_profile configuration file, we can follow the below steps:

$ ls -a

To view the .profile content, use the below cat command:

$ cat .profile

The output shows the built-in & custom environment variables and functions.

This file is used to initialize the current user session on your system, which displays all the log-in configurations of the bash shell.

Differences Between ~/.profile and ~/.bash_profile

Both configuration startup files are used by the logged-in shell users. Owing to a lot of similarities during execution, both the files have differences also, which are listed below: 

~/.bash_profile~/.profile
Executes the startup files specific to bash Shell.  It can read and execute the startup files for all shells without their shell-specific config files. Like, system variables or user-defined variables  ($PATH).
During login, this file automatically runs after the /etc/profile file is executed.During login, if the .bash_profile and .bash_login are missing, this file automatically executes

Conclusion

Several files, such as ~.profile and ~./bash_profile, are executed to load the specific user profile, which runs the customized environment variables and custom functions on startup. The ~.bash_profile is used for the bash shell only, while the ~/.profile is used for every shell, including the bash shell in its absence.