Customizing a Shell Session

Learn how to customize a shell session.

Customizing a shell session

Let’s look at ways to customize our shell session.

When we open a new Bash shell session, our shell executes a series of configuration scripts. We can modify these scripts to customize our environment to our liking. We can set our default terminal editor, create shortcuts for commands, change the information displayed in our prompt, set environment variables, and more.

There are two types of shells: login shells and non-login shells. The type of shell determines which files we use to define the settings. If we put the settings in the wrong place, they won’t apply. This can be a massive source of frustration when trying to set things like environment variables for certain processes, so let’s walk through how each shell works.

When we connect to a machine remotely or physically and provide our username and password, we’re using a login shell. When we do this, Bash first looks at the file /etc/profile, which contains global settings shared by all users. It then looks for the files ~/.bash_profile, ~/.bash_login, or ~/.profile, in that order, and executes the first one it finds. The settings in that file are applied.

Non-login vs Login shells

A non-login shell is a kind that we open when we’ve already logged into the machine and started a shell session. For example, on Ubuntu, we launch the terminal from a graphical desktop. This launches the ~/.bashrc file but not the ~/.profile or ~/.bash_profile files.

Similarly, if we execute the command bash, which will launch a new instance of the Bash shell, the ~/.bashrc file is executed. Again, this is because we’re already logged in, so we’re running a non-login shell.

We can check whether we’re running in a login shell or a non-login shell by looking at the value of the $0 shell variable:

$ echo $0

Use the terminal below to practice this command.

Get hands-on with 1200+ tech skills courses.