BASH Programming

.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:

$ sudo nano .bash_profile

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"

printf "$name\n\n"

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:

#Set a path value

PATH=$PATH:$HOME/temp;

export PATH;

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.

#Set the alias command

alias dt='date'

alias cdir='mkdir'

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

printf "\nToday is "

dt

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.