What’s the Distinction Between .bashrc and .profile on Linux?


fatmawati achmad zaenuri/Shutterstock.com

Logging onto a Linux machine operating Bash causes sure information to be learn. They configure your shell atmosphere. However which information are learn, and when, will be complicated. Right here’s what actually occurs.

The Completely different Kinds of Shell

The atmosphere you get once you launch a shell is outlined by settings held in configuration or profile information. These maintain data that establishes things like your textual content colours, your command immediate, aliases, and the trail that’s looked for executable information once you kind the identify of a program.

There are a variety of various information—in several areas within the file system—the place these settings are saved. However earlier than we get into taking a look at which information are learn once you launch a shell, we have to be clear about what kind of shell you’re utilizing.

A login shell is a shell that you just log into. If you boot your pc and log in, beneath your graphical desktop atmosphere there’s a login shell. If you happen to join to a different pc over an SSH connection, you’ll log right into a login shell too.

The kind of shell you get once you open a terminal window is a non-login shell. You don’t have to authenticate to launch a shell once you’re already logged in. Login and non-login shells are interactive shells. You utilize them by typing directions, hitting the “Enter” key, and studying the on-screen responses.

There are additionally non-interactive shells, too. These are the kind of shells that get launched when a script is executed. The script is launched in a brand new shell. The shebang #!/bin/bash on the prime of the script dictates which shell ought to be used.


echo -e "Howdy, World!n"

This script will probably be run in a non-interactive Bash shell. Word that although the shell is non-interactive, the script itself will be. This script prints to the terminal window, and will simply as simply settle for person enter.

RELATED: 9 Bash Script Examples to Get You Began on Linux

Non-Interactive Shells

Non-interactive shells don’t learn any profile information after they launch. They do inherit atmosphere variables, however they gained’t know something about aliases, for instance, whether or not they’re outlined on the command line or in a configuration file.

You possibly can take a look at whether or not a shell is interactive or not by trying on the choices that had been handed to it as command line parameters. If there may be an “i” within the choices, the shell is interactive. The Bash particular parameter $- incorporates the command line parameters for the present shell.

[[ $- == *i* ]] && echo 'Interactive' || echo 'Non-interactive'

Bash test to indentify interactive and non-interactive shell sessions

Let’s create an alias known as xc that may imply “cat.” We’ll additionally test that we’ve a $PATH variable set.

alias xc=cat
echo $PATH

Setting an alias and echoing the value of $PATH

We’ll attempt to entry each of those from inside this small script. Copy this script into an editor and reserve it as “int.sh.”


xc ~/textual content.dat
echo "Variable=$PATH"

We’ll have to use chmod to make the script executable.

chmod +x int.sh

Using chmod to make a script executable

Let’s run our script:


Running a script that cannot access an alias but can access inherited environment variables

In its non-interactive shell, our script can’t use the alias, however it could actually use the atmosphere variable. Interactive shells are extra fascinating of their use of profile and configuration information.

RELATED: The best way to Set Setting Variables in Bash on Linux

Interactive Login Shells

There are two sorts of interactive login shells. One is the shell that permits you to log in to your pc. On desktops, that is generally the shell underlying your desktop atmosphere. Whether or not you utilize a windowed or tiling desktop atmosphere, one thing has to authenticate you with the Linux system and allow you to log in.

On servers with no desktop atmosphere put in, you log in instantly into an interactive shell. You are able to do the identical form of factor on a desktop pc should you drop out of the desktop atmosphere and entry a terminal. On GNOME you are able to do this with the Ctrl+Alt+F3 key mixture. To return into your GNOME session press the Ctrl+Alt+F2 key mixture. The shell you hook up with over an SSH is a login shell too.

The profile and configuration information which are known as will be set utilizing atmosphere variables, to allow them to fluctuate from distribution to distribution. Moreover, not all information are utilized by each distribution. In a generic Bash set up, interactive login shells learn the “/and many others/profile” file. This holds system-wide shell configuration choices. In the event that they exist, this file additionally reads information corresponding to “/and many others/bash.bashrc” and “/usr/share/bash-completion/bash_completion”.

Bash then seems to be for a “~/.bash_profile” file. If it doesn’t exist, Bash seems to be for a “~/.bash_login” file. If that file doesn’t exist, Bash tries to discover a “.profile” file. As soon as one among these information is discovered and skim, Bash stops looking out. So generally, “~/.profile” is unlikely to be learn in any respect.

Usually, you’ll discover one thing like this in your “~/.bash_profile” or, as a form of backstop, in your “~/.profile” file:

# if operating bash
if [ -n "$BASH_VERSION" ]; then
  # embrace .bashrc if it exists
  if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"

This checks that the lively shell is Bash. Whether it is, it searches for a “~/.bashrc” file and reads it if one is discovered.

Interactive Non-Login Shells

A Bash interactive non-login shell reads “/and many others/bash.bashrc” after which reads “~/.bashrc” file. This permits Bash to have system-wide and user-specific settings.

This conduct will be modified with compilation flags when Bash is compiled, however it could be a uncommon and peculiar circumstance to come across a model of Bash that doesn’t supply and skim the “/and many others/bash.bashrc” file.

Every time you open a terminal window in your desktop, these two information are used to configure the atmosphere of that interactive, non-login shell. The identical factor occurs for shells launched by purposes, such because the terminal window within the Geany IDE.

The place Ought to You Put Your Configuration Code?

One of the best place to place your private customization code is in your “~/.bashrc” file. Your aliases and shell capabilities will be outlined in “~/.bashrc”, they usually’ll be learn in and out there to you in all of the interactive shells.

In case your distribution doesn’t learn your “~/.bashrc” in login shells, and also you’d prefer it to, add this code to your “~/.bash_profile” file.

# if operating bash
if [ -n "$BASH_VERSION" ]; then
  # embrace .bashrc if it exists
  if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"

Modularity Is Finest

In case you have numerous aliases, otherwise you wish to use the identical aliases throughout plenty of machines, it’s best to retailer them in their very own file, and the identical along with your shell capabilities. You possibly can name these information out of your “~/.bashrc” file.

On our take a look at pc, the aliases are saved in a file known as “.bash_aliases” and a file known as known as “.bash_functions” holds the shell capabilities.

You possibly can learn them from inside your “~/.bashrc” file like this:

# learn in my aliases
if [ -f ~/.bash_aliases ]; then
  . ~/.bash_aliases

# learn in my shell capabilities
if [ -f ~/.bash_functions ]; then
  . ~/.bash_functions

This allows you to simply transfer your aliases and capabilities between computer systems simply. You simply want so as to add the strains above to the “~/.bashrc” file on every pc and duplicate the information containing your aliases and shell capabilities to your property listing on every pc.

It means you don’t want to repeat all the definitions from the “~/.bashrc” on one pc to the “~/.bashrc” information on every of the opposite computer systems. It’s additionally higher than copying your whole “~/.bashrc” file between computer systems, particularly if they’re operating Bash on totally different distributions.

In Abstract

The information you actually need to find out about are:

  • /and many others/profile: System-wide configuration settings. Utilized by login shells.
  • ~/.bash_profile: Used to carry settings for particular person customers. Utilized by login shells.
  • ~/.bashrc: Used to carry settings for particular person customers. Utilized by interactive non-login shells. May also be known as from your “~/.bash_profile” or “~/.profile” file for login shells.

One handy technique is to place your private settings in “~/.bashrc”, and ensure your “~./bash_profile” file calls your “~/.bashrc” file. Meaning your private settings are held in a single single file. You’ll get a constant shell atmosphere throughout login and non-login shells. Combining this with storing your aliases and shell capabilities in non-system information is a neat and strong answer.

Supply hyperlink