Caltech Home > Caltech HPC Homepage > Documentation > FAQ > How do I modify my shell environment?
Search open search form

How do I modify my bash environment?

You may want to change the way things look and  act when you log into the system.  You can make all sorts of changes to make things easier for you to use the cluster

What can I change?

Pretty much anything!  Many people will change their prompts, the colors things display in, their paths to make launching things easier, aliases to make commands easier to run, default editors, etc.

Here we will discuss things that many people prefer to change

Making It look the way i want
colorize "ls"

One popular setting is to colorize the output of the "ls" command so that different file types show up differently.  Luckily the authors of ls have already thought of it and have an option to do just that:

ls --color=auto

To make this happen whenever you run it, you can make it an alias:

alias ls="ls --color=auto"

Setting the prompt

Many people like to change there shell prompt to give them useful information.  Most common choices are showing the hostname, username, or directory you are in, which is default on the central cluster.  This can be changed by setting the "PS1" variable to whatever you want.  To set it to  "[username@hostname directory]$"  you would set it as follows:

export PS1="[\u@\h \W]\$"

Here are the most common things people put into there prompt:

  • \d    The date, in "Weekday Month Date" format (e.g., "Tue May 26").
  • \h    The hostname, up to the first ‘.’.
  • \H   The hostname.
  • \t   The time, in 24-hour HH:MM:SS format.
  • \T  The time, in 12-hour HH:MM:SS format.
  • \A  The time, in 24-hour HH:MM format.
  • \u  The username of the current user.
  • \w  The current working directory, with $HOME abbreviated with a tilde
  • \W The basename of $PWD, with $HOME abbreviated with a tilde.
  • \!   The history number of this command.
  • \$   If the effective uid is 0, #, otherwise $.

You can see the full list of choices in the bash manual

Making is behave the way i want it to

A very common thing people like to set it aliases.  Think of them as a shortcut to do a long long command easily. Essentially you just alias a command to a different command.

alias  ll="ls -l --color=auto"
alias mydir="cd ~/some/deeply/buried/directory"

Default editor

some commands will want  you to edit files when running commands and will open up documents in an editor.  You may want to use your favorite editor rather than the default.  Common editors include vim, emacs, and nano.  Nano is a easy to use editor that you may want to try if you are just getting started.  If you are already experienced, you probably already have a favorite. To set the default set the EDITOR environment variable:

export EDITOR="nano"

Setting my PATH

The path variable is used to look in directories for executables in a certain order.  If you compile software in you home directory, for example, you will likely want to be able to run the software without the fully qualified path.  In this case you would add it to your PATH environment variable. You will almost always want to keep the PATH already set as part of your path so that standard tools still work.  to do this, always make sure you have $PATH as part of the new path.

export PATH=/home/user/bin:$PATH

How do i make them permanent?

Everything mentioned so far takes place in the shell you are running in.  This is a good way to test things.  For example, if you change you path in shell and can no longer run commands, just close the shell and open a new one. You probably, however, want these changes to take place in all shells you open.  To do this, you will add the commands to one of 2 files.  Either the ".bash_profile" or ".bashrc".  These files are in you home directory.  Notice that they both have a "." in front of them.  This hides the files in a normal listing.

Which one?

Depends on what kind of shell you want it for.  There are login shells which are typically used interactively, and non login shells which get called from scripts and the like. A good sign it is a login shell is if you had to provide a password to get it.

Typically,  when you login into the cluster, you will be in a login shell.  These shell use the .bash_profile.  This is a great place to put prompt changes and colorization options.

When not logging in and authenticating, you would usually get a non login shell.  These read the .bashrc file.  This can be confusing when running scripts, using screen, or in your jobs.

In addition, non-login shells occur any time you run a script that uses bash. Non-login shells read from the .bashrc file.

For most users You will want to have the .bash_profile read in the .bashrc.  When doing this, paths set in .bashrc should also be set in a login shell.  The default files you get when first logging in are set to do just that.  If for some reason you delete it, you can  add the following to the top of your .bash_profile to get the behaviour back

if [ -f ~/.bashrc ]; then
   source ~/.bashrc

Putting is all together


# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
	. ~/.bashrc

# User specific environment and startup programs

PS1="[\u@\h \W]\$"
export PATH PS1


# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc

# Uncomment the following line if you don't like systemctl's auto-paging feature:

# User specific aliases and functions
# colorize ls"
alias ls="ls --color=auto"
# long listing in ls
alias ll="ls -l --color=auto"