- ~/.bash_profile | Explained
- What is the Hierarchy of Startup Files in Linux?
- What is the ~/.bash_profile Command in Linux?
- How to Use ~/.bash_profile in Linux?
- How to Use the ~/.bash_profie With .bashrc File?
- Conclusion
- .Bash_Profile File in Bash
- Benefits of Using the .Bash_Profile or .Profile File
- Different Uses of .Bash_Profile or .Profile File
- Example 1: Set a Variable with a Default Value
- Example 2: Set a New PATH Location
- Example 3: Add the Short Form of the Commands
- Example 4: Print the Current Date and Time in the Startup
- Conclusion
- About the author
- Fahmida Yesmin
- How to Reload .bash_profile in Linux Command-Line
- The .bash_profile File
- Creating the .bash_profile File
- Edit the .bash_profile File
- Customizing the .bash_profile File
~/.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?
- What is ~/.bash_profile in Linux?
- How to Use ~/.bash_profile in Linux?
- Usage of ~/.bash_profie With .bashrc File?
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:
Furthermore, you can view the “.profile” file by using the cat command:
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:
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:
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:
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:
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:
The user can also view the content of the “.bashrc” file using the “cat” command:
Open the “.bashrc” file in the nano editor and add the echo statement as shown:
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.
.Bash_Profile File in Bash
Bash has many shell script files to set the system configurations. The “.bash_profile” file is one of them. It is a hidden file and it executes every time when the user logs in to the system. Each user has his/her “.bash_profile” file. When a new user account is created, a new “.bash_profile” file is created for that user. This file contains all configuration data of the shell for the currently logged in user, such as the command history, command aliases, environment variables, etc. Many types of settings can be set or unset by editing this file.
Benefits of Using the .Bash_Profile or .Profile File
The “.bash_profile” or “.profile” file is mainly used to set the environment variable for the user or define any script to perform the automated tasks at the startup of the system. The “.bashrc” file can be used for the same purpose. If you change any setup and wants to display the output at the startup only and you don’t want to see the changes every time you open the terminal, it is better to modify it to the “.bash_profile” or “.profile” file instead of the “.bashrc” file.
Different Uses of .Bash_Profile or .Profile File
If you don’t get the “.bash_profile” file, search the “.profile” file in the system. Run the following command to open the file to edit with the root permission:
Example 1: Set a Variable with a Default Value
Add the following lines at the end of the file. A variable is exported with the string value and the printf command is used to print the value of the variable in the terminal at the startup of the system:
#Set the variable with the default value
export name = «Welcome to Linux world»
If the system is restarted and a terminal is opened, the following output appears. The value of the $name variable is printed in the output:
Example 2: Set a New PATH Location
The $PATH variable is used to add the new path location to the existing value of the $PATH variable.
Open the “.bash_profile” file and add the following content at the end of the file. Here, a new path, $HOME/temp, is added in the $PATH variable. This variable is exported and printed later:
printf «The value of the PATH variable: \n $PATH \n «
If the system is restarted and a terminal is opened, the following output appears. The new path location that is added in the $PATH variable is shown in the output. The new path is “/home/fahmida/temp”.
Example 3: Add the Short Form of the Commands
The “alias” command is used to create the short form of any command. If it is required to execute any long command frequently, it is better to create the shortcut of that long command with the “alias” command and use the short form of the command when required. Two simple uses of this command are shown in this example to know the use of the command. The “date” command is used to print the current date and time value while the “mkdir” command is used to create a new directory. Open the “.bash_profile” file and add the short form of these commands at the end of the file.
Restart the system and open the terminal. The use of the “mkdir” command is shown in the following output. The “cdir” command is created as the alias command of the “mkdir” command. Here, a new directory named “tempdir” is created using the “cdir” command. Next, the “ls” command is executed to check whether the new directory is created or not.
Example 4: Print the Current Date and Time in the Startup
Open the “.bash_profile” file and add the following lines to print the current date and time at the startup of the terminal. Here, the short form of the “date” command is used to print the current date and time in the startup that is created in the previous example.
#Display the current date and time
Restart the system and open the terminal. The current date and time are printed in the output to execute the “dt” command.
Conclusion
Many automated tasks like running a script, opening an application, and others can be done very easily using the “.bash_profile” or “.profile” file. Simple uses of this file are shown in this tutorial using different types of examples such as setting the $PATH variable, using different commands, etc. The purposes of using the “.bash_profile” file will be cleared after reading this tutorial.
About the author
Fahmida Yesmin
I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.
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:
- Modification of the Linux operating system working environment by configuring and editing terminal settings, and custom environment variables.
- 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:
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.
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.
Customizing the .bash_profile File
Let us open and modify the .bashrc file we viewed from the ls -la command.
At the bottom of this file, add an echo statement with the following details:
echo "This is a 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.
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.
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.
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.
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.