[email protected]Mon – Fri 9:00AM – 5:00PMLinkedInXFacebook
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 Implement Network Segmentation and Resource Isolation in Linux.
February 6, 2024

How to Implement Network Segmentation and Resource Isolation in Linux.

How to Implement Network Segmentation and Resource Isolation in Linux. Securing and managing networks in a Linux environment involves advanced practices like network segmentation and resource isolation. This guide explores these concepts, providing detailed instructions and examples for…

Read article
How to open or close ports in Linux.
February 6, 2024

How to open or close ports in Linux.

How to open or close ports in Linux. Effectively managing ports on your Linux system is crucial for both security and service accessibility. This comprehensive guide will provide a detailed walkthrough on opening and closing ports using the robust tools iptables and ufw. Understanding these…

Read article
How to use ypdomainname command.
January 23, 2024

How to use ypdomainname command.

How to use ypdomainname command. In the realm of Linux networking, the ypdomainname command plays a crucial role in the context of the Network Information Service (NIS). NIS, formerly known as Yellow Pages, is a distributed database service that facilitates the sharing of network configuration and…

Read article
Get in Touch

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

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

Get Started →Contact Us
[email protected]Mon – Fri 9:00AM – 5:00PM