Configuring shell behavior
Your shell is the most important Linux program you use because it provides access to your other programs and files. You want your interactions with bash to be comfortable and productive.
Bash can be configured in many ways to provide the Linux experience you want and to let you work quickly and easily. Regular Linux users make customizing bash a priority task when logging into a new computer (often by copying their preferred configuration from another machine).
The .bashrc
file
You begin a shell session each time you log into a computer or open a new terminal window. Shell sessions are independent of one another. The work you did yesterday (or a few minutes ago) doesn't affect your new session.
You may want every shell session to start with certain actions - tweaking a
setting or running a start-up command. You can tell bash to take these actions
at the start of every session (without retyping them) by editing a special
configuration file named .bashrc
. You find this file in your
home directory (many Linux systems create it for you when your account is
set up). You can check for it using ls -a
(you need -a
to see it becaues it's a dotfile), or open it in a text editor.
The text of .bashrc
is a shell script, a program written
with shell commands. Bash runs the script (and performs each command) at the
start of every shell session. You can add commands (or edit existing ones) to
customize your shell sessoin.
Example: Welcoming words of wisdom from a cow
Not all Linux commands are serious business! Two light-hearted commands intalled on some Linux systems are:
fortune
- print a random, hopefully interesting, adagecowsay
- configurable speaking/thinking cow (and a bit more)
Connecting these two commands with a pipe results in an ASCII-art cow speaking your fortune:
If you add this command to your .bashrc
, it will run at the
start of every shell session - welcoming you with bovine words of wisdom!
Changes to your .bashrc
(notably, to environment variables like
your PATH
) don't automatically apply to the current session. The
source
command is used to re-run the file in the current shell.
source
- execute commands from a file in the
current shell
$ source filename
The newest version of .bashrc
is automatically used when you
start a new session - no source
required.
Historically, bash (and other shells) distinguished between two operating modes:
- login shells which are launched when you first log into or connect to (e.g., with ssh) the computer
- non-login shells which are launched from another shell instance during a work session
Since this distinction sometimes matters, bash uses the .bash_profile
(or shell-independent .profile
) to configure login shells and
.bashrc
to configure non-login shells.
Most .profile
implementations source .bashrc
, so
you can (usually) ignore this distinction and put everything in
.bashrc
unless you're doing more involved shell configuration.
Customizing your shell
The customizations in this section can be typed directly into your shell prompt
to customize the current session. They are also frequently added to the
.bashrc
file so they take effect automatically in every session.
Command aliases
As you spend more time working with Linux, you may find yourself typing the same command - maybe with specific flags or arguments - again and again (and again...). An alias is a user-defined "command" that stands for another (usually longer) command, like a desktop shortcut to your favorite program.
The alias
command is used to define a new alias, or to print
the current definition of an existing alias.
alias
- print or define aliases
$ alias [name[=value]...]
The value
is a command (including any command line
options or arguments) enclosed in quotation marks. Whitespace matters to
bash, so make sure there are no spaces between the name
and equals sign or between the equals sign and value
.
If you type the name of an existing alias without a matching value, bash will
print the current definition of that alias. If you type alias
with no other arguments, bash will print all currently-defined aliases and
their definition.
Try it yourself: Common bash aliases
Bash also uses system-level configuration files to get set up,
including some that define aliases. What aliases are already defined
for you on cs-class? (Hint: connect to cs-class and run the
alias
command.)
A newly-created cs-class account (i.e., one that hasn't defined any personal aliases yet) will have the following pre-defined ones:
Configuring your PATH
Your PATH
environment variable tells
bash where to look for commands you run. Your PATH
is initially set
by a system-wide script to include the location of common commands
(such as cp
).
You may want your PATH
to include other directories containing
programs you've installed (or written yourself). You can modify your
PATH
with a line in your .bashrc
file. The syntax
looks like string concatenation and variable assignment in a programming langauge
(because that's exactly what it is).
A typical PATH
assignment in a
.bashrc
file
export PATH=$PATH:
/path/to/new/directory
Breaking down this line piece-by-piece:
- The
PATH
(on the left side of the assignment) tells bash to assign a value toPATH
- The
=
assignment operator must not have spaces on either side - The
$PATH
(on the right side of the assignment) tells bash to use the current value ofPATH
(note the dollar sign here!) - The colon (right after
$PATH
separates entries in the path - The remainder of the line is the directory you're adding to the path
- The
export
at the beginning ensures the new value ofPATH
is available to other programs started by bash.
This adds a new directory to the back of the PATH
so bash will
look there after searching the other directories in the path. The order of
the concatenation matters - if you reverse them, bash would look in the new
directory first because it's at the front of the path.
Example: Adding a newly-installed program to
PATH
I recently installed OpenMPI 4.1.1 (a library for writing parallel
computer programs) on my personal laptop. OpenMPI is distributed as source
code, which is configured and compiled by the user (me) based on their
machine (this is common in the Linux world). This created a new directory
(~/opt/openmpi-4.1.1/bin
) containing the commands for parallel
programs (like mpicc
and mpirun
).
After installation, I added the following line to my .bashrc
:
export PATH=$PATH:$HOME/opt/openmpi-4.1.1/bin
This tells bash to search for the command mpirun
in the correct
directory.
(You'll notice I didn't write ~
for my home directory here.
Bash does not perform tilde expansion inside scripts, so I had to use the
evironment variable $HOME
(which stores the path to my home
directory) instead.)
Customizing your prompt
Your shell prompt is where you type every command you give to bash. It can also give you information about the current state of your shell or computer. Typical shell prompts include your username, the machine hostname, and the current working directory.
A typical Linux shell prompt (on class-1)
[re268@cs-class-1 ~]$
Your prompt's appearance is described by an environment variable named
PS1
, whose contents you can view using echo
.
The PS1
value for a typical Linux shell prompt
[re268@cs-class-1 ~]$ echo $PS1
[\u@\h \W]\$
The value of PS1
is mostly literal characters, with special
escaped characters representing special information.
\u
is your username\h
is the first part of the hostname ("cs-class-1")\W
is the name of the current working directory\$
is "$
" if you're not root, and "#
" if you are
You can assign a new value to PS1
that includes the current
date or time; displays information about the current directory (such as the
status of a git repository); and uses colors for different parts of the prompt.
The Web is full of examples of such prompts to suit your individual tastes.
Changing your default shell
Shells are regular programs and not part of Linux. This means you can swap one shell for another to change how you interact with Linux (or even write your own shell program!).
Bash is arguably the most popular shell and is the default on many Linux computers. Some other widely-available shells include:
- tcsh (a successor to csh), also called the "C shell". Written by Bill Joy in 1978, this shell uses a syntax closer to the C programming language when writing shell scripts (programs consisting of shell commands).
- ksh, the "Korn shell" was written by David Korn in 1983. This shell was proprietary ATT software for many years, limiting its use outside certain Unix distributions.
- zsh is a "newer" shell (written in 1990) which aimed to combine the features of tcsh and ksh. It was adopted as the default shell in macOS Catalina (10.15) in 2019 (replacing bash)
- fish calls itself a "friendly interactive" shell and was first released in 2005. Fish is an exotic shell - it does not adhere as closely to the POSIX standard that other shells follow.
You can use the chsh
command to select which shell you want to
use (or reset to the system default).
chsh
- change login shell
$ chsh [options] [login]
You must provide the path to a valid login shell for the chsh
command to succeed. A list of available (valid) login shells is kept in
the /etc/shells
file - you can cat
this file to
see the list.