[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Knowledge BaseSeptember 4, 2023

.bashrc vs .bash_profile in Linux

.Bashrc Vs .Bash_profile 

In this article, we will talk about the Bash startup files and the difference between the .bashrc and .bash_profile files.

 

Interactive Login and Non-Login Shell

When invoked, Bash reads and executes commands from a set of startup files. What files are read depends upon whether the shell is invoked as an interactive login or non-login shell.

A shell can be interactive or non-interactive.

In simple terms, an interactive shell is a shell that reads and writes to a user’s terminal, while a non-interactive shell is a shell that is not associated with a terminal, like when executing a script.

A login shell is invoked when a user login to the terminal either remotely via ssh or locally, or when Bash is launched with the –login option. An interactive non-login shell is invoked from the login shell, such as when typing bash in the shell prompt or when opening a new Gnome terminal tab.

 

Bash Startup Files

When invoked as an interactive login shell, Bash looks for the /etc/profile file, and if the file exists , it runs the commands listed in the file. Then Bash searches for ~/.bash_profile, ~/.bash_login, and ~/.profile files, in the listed order, and executes commands from the first readable file found.

When Bash is invoked as an interactive non-login shell, it reads and executes commands from ~/.bashrc, if that file exists, and it is readable.

 

Difference Between .bashrc and .bash_profile

.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 any startup file is not present on your system, you can create it.

 

Conclusion

.bash_profile and .bashrc are files containing shell commands that are run when Bash is invoked. .bash_profile is read and executed on interactive login shells, while .bashrc on non-login shells.

Keep reading
How to use Whoami Command
March 12, 2023

How to use Whoami Command

How To Use Whoami Command In this article, we will cover the whoami command. As its name suggests, the whoami command prints the user name of the effective user ID. In other words, it displays the name of the currently logged-in user. How to Use the whoami Command The syntax for the whoami command…

Read article
How to Mount Windows Share on Linux using CIFS
March 10, 2023

How to Mount Windows Share on Linux using CIFS

How To Mount Windows Share On Linux Using CIFS In this tutorial, we will explain how to manually and automatically mount Windows shares on Linux systems. The Common Internet File System (CIFS) is a network file-sharing protocol. CIFS is a form of SMB. On Linux and UNIX operating systems, a Windows…

Read article
How to use W command
March 10, 2023

How to use W command

How To Use W Command In this article, we will talk about the w command. w is a command-line utility that displays information about currently logged in users and what each user is doing. It also gives information about how long the system has been running, the current time, and the system load…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00