.bashrc and aliases

Overview

Teaching: 5 min
Exercises: 5 min
Questions
  • How do I modify the .bashrc file?

Objectives
  • Customize your bash experience.

  • Define aliases to save you time and typing.

.bashrc

The bash shell allows for a great deal of customization including defining shortcuts for frequently used commands. Such preferences are defined in a file in your home directory called .bashrc, which is a shell script. It’s used to save and load your terminal preferences and environmental variables. In order to load your preferences, bash runs the contents of the .bashrc file at each launch.

Some applications will modify your .bashrc file when they are installed or initiated. For instance, if you use Anaconda to manage the installation of personal Python or R libraries on your COLA account, it will add some scripting code to your .bashrc file so that it starts up properly when you login.

Let’s take a look at the contents of your .bashrc file. Go to your home directory…

$ cd
$ ls -l .bashrc
-rw------- 1 jdoe123 users 520 Aug 21 13:16 .bashrc

You will see something like the result above.

A bit about the information shown when you perform a verbose file listing (i.e., using the -l option):

Note that files with names starting with a period are usually system files and are, by default, “hidden”. They will not show up with the ls command unless the -a option is used or the file is explicitly named as we did above.

Adding aliases to .bashrc

Edit .bashrc with your preferred editor.

Let’s add some aliases. An alias is a command name that is defined to execute another command, set of commands (e.g., using pipes), or execute a script. You can also redefine an existing command name to have a different behavior, e.g., to make certain command options act as the defaults when they ordinarily are not. For example, you could redefine the rm command with the alias rm -i so that typing simply rm would always ask for confirmation before deleting files.

You could add the following lines at end of the .bashrc. file, which will use the alias` command to define aliases:

alias ls="ls -qx --color=always"
alias ll="ls -al --color=always"
alias lt="ls -alt --color=always | head -10"

The alias command defines a command as the name on the left side of = everything that is in the quotation marks on the right, which enclose the command(s) as you would have typed it on the command line. Be sure there are no spaces on either side of =. This is a quirk of the bash language protocol.

Save the file. The changes will not take effect in your current shell until you re-execute the commands in the .bashrc file. You do this with the source command (Note that a logging out and back in again will also achieve this because bash runs the contents of the .bashrc file at each launch):

$ source .bashrc

Now try the commands and see how they look!

$ ls

To see a list of the aliases you have defined, use the alias command with no arguments:

$ alias
alias ls='ls -qx --color=always'
alias ll='ls -al --color=always'
alias lt='ls -alt --color=always | head -10'

You can also define an alias by directly typing its definition on the command line. But once you log out (or if your connection is dropped), the alias definition would disappear.

You can make many customizations in addition to defining aliases. You can define the list directories that are in your default PATH when conducting searches for executables, change the way your command line prompt appears, or preload software libraries, to name a few.

Key Points

  • Unix shells can be launched in a customized way with the user’s preferences.

  • Aliases can be defined that substitute short strings for long or complex commands.