Skip to Content

Understanding bash_profile file in Linux

A bash profile is a file that contains commands that are executed every time you open a new terminal window or log in to your Linux server. In this blog post, we will discuss the purpose of the bash_profile file and how to create and use it. We will also provide some tips and tricks for optimizing your Bash profile.

Understanding bash_profile file in Linux

The bash_profile file is located in the user’s home directory and is typically hidden from view. The bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells. When you log in (type username and password) via console, either sitting at the machine or remotely via ssh,.bash_profile is executed to configure your shell before the initial command prompt.

To view the contents of your bash_profile, you can use the cat command: cat ~/.bash_profile

If you do not have a bash_profile file, you can create one using a text editor like nano or vim. The contents of the file are executed every time you open a new terminal window or log in to your Linux server. You can add any commands that you want to be executed automatically, such as setting environment variables or creating aliases.

Tips to use bash_profile file in Linux

Here are some tips for creating and using your Bash profile:

  • Use comments to annotate your code and make it easier to read. Comments start with a # symbol and are not executed as commands.
  • Use aliases to create shortcuts for frequently used commands. For example, you can alias the ls command to ll so that it will be executed whenever you type ll in the terminal.
  • Set environment variables in your Bash profile so that they will be available every time you open a new terminal window. Environment variables can be used to store information such as your username, home directory, or favorite editor.
  • Use functions to group related commands together. Functions are especially useful if you need to execute a sequence of commands often.

 

Difference Between .bashrc and .bash_profile in Linux

.bash_profile is read and executed when Bash is invoked as an interactive login shell, while .bashrc is executed for an interactive non-login shell. Use .bash_profile to run commands that should run only once, such as customizing the $PATH environment variable .

Put the commands that should run every time you launch a new shell in the .bashrc file. This include your aliases and functions , custom prompts, history customizations , and so on.Typically, ~/.bash_profile contains lines like below that source the .bashrc file. This means each time you log in to the terminal, both files are read and executed.

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

Most Linux distributions are using ~/.profile instead of ~/.bash_profile. The ~/.profile file is read by all shells, while ~/.bash_profile only by Bash.

If you have any questions about bash profiles or would like help creating one, feel free to reach out to our team of Linux experts. We are always happy to help!

Michaeldyere

Saturday 31st of December 2022

Great article. Thanks.