Editing your .bashrc – FASRC DOCS (original) (raw)

Why modify your .bashrc?

There may come a time when you want to customize your default login environment. If you always load the same module when logging in, or want to add other definitions for your login shell to process, that means editing the .bashrc file in your home directory. This runtime configuration file is processed (or ‘sourced’) by bash, the default shell on the FASRC cluster, when you log into a node and by your jobs when they run. Any time a new shell is created by your user, this file is sourced.

As you might imagine, you need to be very careful not to break your .bashrc or you may not be able to log in. If that should happen, contact us and we can copy in a default .bashrc for you so you’re able to log in again.

The default FASRC .bashrc file contains the following:

.bashrc

Source global definitions

if [ -f /etc/bashrc ]; then . /etc/bashrc fi

User specific aliases and functions

After the # User specific aliases and functions section is where you should put any customizations of your .bashrc as that will cause them to run after the global definitions have been sourced.The # Source global definitions section is where the default cluster-wide definitions are sourced. The /etc/bashrc file includes definitions necessary to make your jobs and other interactions run properly across the cluster. Please do not remove this section as you will run into problems as a result. For instance, your .bashrc may contain the old module definitions calls. If you see source new-modules.sh, that’s what that is. You can leave or remove is as it’s benign, but we recommend removing it to avoid any confusion.

Editing your .bashrc

In order to edit your .bashrc, you’ll need to be comfortable with a command-line editor such as nano (probably the easiest to get started with) or vim (aka vi). You may also be able to edit the file using your SFTP client of choice, but experiences may vary. We’ll assume here that you’re using nano.

From a login or other node on the cluster, type nano ~/.bashrc to open the file in the nano editor. My .bashrc has already been added to, so you’ll see additional definitions below the# User specific aliases and functions section.

Let’s look at what those additions do and how they fit into the larger bash login environment. And we’ll add one more.``

> THE GLOBAL DEFINITIONS
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi> THE USER’S DEFINITIONS
# User specific aliases and functions
>> MY CODE TO CHECK WHETHER I’M LOGGING INTO A SPECIFIC SERVER
# Check to see if I'm logging into a particular node
#
if [ <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>H</mi><mi>O</mi><mi>S</mi><mi>T</mi><mi>N</mi><mi>A</mi><mi>M</mi><mi>E</mi><mo>=</mo><mo>=</mo><mi mathvariant="normal">&quot;</mi><mi>s</mi><mi>o</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>o</mi><mi>d</mi><mi>e</mi><mn>01.</mn><mi>r</mi><mi>c</mi><mi mathvariant="normal">.</mi><mi>f</mi><mi>a</mi><mi>s</mi><mi mathvariant="normal">.</mi><mi>h</mi><mi>a</mi><mi>r</mi><mi>v</mi><mi>a</mi><mi>r</mi><mi>d</mi><mi mathvariant="normal">.</mi><mi>e</mi><mi>d</mi><mi>u</mi><mi mathvariant="normal">&quot;</mi><mo stretchy="false">]</mo><mi mathvariant="normal">∣</mi><mi mathvariant="normal">∣</mi><mo stretchy="false">[</mo></mrow><annotation encoding="application/x-tex">HOSTNAME == &quot;somenode01.rc.fas.harvard.edu&quot; ] || [ </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.08125em;">H</span><span class="mord mathnormal" style="margin-right:0.10903em;">OSTN</span><span class="mord mathnormal">A</span><span class="mord mathnormal" style="margin-right:0.05764em;">ME</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">==</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">&quot;</span><span class="mord mathnormal">so</span><span class="mord mathnormal">m</span><span class="mord mathnormal">e</span><span class="mord mathnormal">n</span><span class="mord mathnormal">o</span><span class="mord mathnormal">d</span><span class="mord mathnormal">e</span><span class="mord">01.</span><span class="mord mathnormal">rc</span><span class="mord">.</span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">a</span><span class="mord mathnormal">s</span><span class="mord">.</span><span class="mord mathnormal">ha</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">d</span><span class="mord">.</span><span class="mord mathnormal">e</span><span class="mord mathnormal">d</span><span class="mord mathnormal">u</span><span class="mord">&quot;</span><span class="mclose">]</span><span class="mord">∣∣</span><span class="mopen">[</span></span></span></span>HOSTNAME == "somenode02.rc.fas.harvard.edu" ] ; then
>> IF YES, THEN SOURCE A SHORT SET OF ADDITIONAL DEFINITIONS
# Interactive login, just load a short set of definitions
# Make sure nothing prints any output as that will cause SFTP to hang ``
source ~/.bashrc_simple
else
>> IF NO, THEN SOURCE MY FULL SET OF DEFINITIONS (MODULES, etc.)
# Normal login or jobs will get the full set of definitions
source ~/.bashrc_full
fi
# fi ends the if statement
>> DONE. FI IS THE BASH COMMAND THAT TERMINATES AN IF STATEMENT >> I HAVE A SET OF ALIASES THAT I ALWAYS WANT SOURCED NO MATTER WHERE I LOG IN
# Always source my custom aliases file last
#
source ~/.aliases>> LET’S SAY I WANT TO ALWAYS LOAD THE GIT MODULE REGARDLESS OF WHERE I LOG IN >> YOU CAN ADD A MODULE LOAD STATEMENT AT THE END (NOT RECOMMENDED)

` # Always load the git module when I log in

module load git
`
>> BUT IT’S BETTER TO PUT ALL YOUR MODULE LOADS INTO A FUNCTION >> THEN ANY TIME YOU NEED TO LOAD THEM ALL, YOU SIMPLY CALL THE FUNCTION
# Load my module favorites by invoking ‘mymodules’

function mymodules { `` module load git module load Anaconda/5.0.1-fasrc01 }

You’re free to experiment with your .bashrc, but please do remember that you can break your access while doing so. A good practice to mitigate this is to open two shells before editing your .bashrc so that, if you break your login environment, you’re still logged into that other shell (.bashrc is only sourced on login, so that shell will be ‘normal’) and can likely fix or revert the damage from there. It’s a good idea to make a copy of your working .bashrc first before editing it.

If SFTP is failing because your .bashrc causes output to STDOUT (echo, etc.)

You can try wrapping your interactive needs inside a statement that tests for an interactive shell. This may not work 100% of the time:
`
if [ -z "$PS1" ]; then

non-interactive stuff, if any, here

else

interactive stuff here

fi
`

Bash
Intro to Software on the FASRC cluster