Articles > August 1, 2023 Iván Díaz, Ph. D; Erica Bianco, Ph. D

What not to add to your ~/.bashrc: good practices for the end user

Motivation

The ~/.bashrc is a configuration file for the interactive bash shell. As a user, you might find it useful to add some lines of code to your ~/.bashrc file (or ~/.bash_profile) to not have to type them in every time you log into your machine. The Internet is full of information about the ~/.bashrc file, providing examples of what to add to it and solutions/workarounds for issues that include adding something to your ~/.bashrc, and here comes the bad news: some of these additions can do harm. 

Depending on what you add to your ~/.bashrc, and how you use it, it may cause different issues that make life harder for the end user support staff that help you fix them.

Below, we provide some suggestions on what to add and what not to add to your ~/.bashrc.

Further discussion and more suggestions can be asked and found on the HPCKP Forum – End User support.

What not to add to your ~./bashrc

As a rule of thumb: add nothing to your ~/.bashrc and keep it as clean as possible. 

Below, some issues that you can run into if you change your ~/.bashrc.

Exporting variables

Exporting is bad for the environment. Exporting environmental variables in your ~/.bashrc can cause differences in the behavior of the commands you run depending on the environment they are executed from. 

If your programs behave unexpectedly, check the environment variables by running the env program. 

Here some examples: 

PATH

Having a hardcoded PATH variable instead of a wrapper script can cause problems when included in ~/.bashrc:

  • Changing the PATH variable in ~/.bashrc changes the order in which directories are queried when looking for a binary, and you might end up executing a different binary than the intended one.
  • In a batch scheduler environment, a script that works correctly on interactive nodes may fail when sent to compute nodes.

LD_LIBRARY_PATH

Some issues may appear with dynamic libraries when the LD_LIBRARY_PATH is hardcoded in the ~/.bashrc. If an application needs to override the standard libraries, this should be done in a wrapper that does not pollute the environment, and makes other binaries fail. 

SENSITIVE DATA IN VARIABLES

If you store sensitive data (passwords, tokens) in ~/.bashrc (or other script files), it can be exposed in unintended ways: other unrelated applications/commands can include the environment variables in the log and expose your tokens. Sensitive data should only be in effect when running the intended application/command.

Loading modules

Under Lmod/Environment Modules, adding the “module load XXX” to the ~/.bashrc might cause different kinds of errors; for example, if the version is not specified, the latest version will be loaded, and it might behave differently or have different flags/expected output.

In general, this defeats the purpose of having a module system in which different versions can be loaded depending on the current needs.

Unintended ~/.bashrc modifications

Some applications might modify your ~/.bashrc in order to be self-loaded when entering your environment. Check your ~/.bashrc file after installing new packages like Miniconda.

Very long login/connection waiting time

A long list of commands in your ~/.bashrc file can take an appreciable amount of time to run. Each login you do with that account/home directory will take longer than expected. This includes any accidental disconnection due to VPN/network problems, and may give timeout errors, impeding the connection to the server. 

What you can add to your ~/.bashrc

So, what can you add to your ~/.bashrc to make it useful? What can you add to improve your user experience? 

Here is a list of some elements that can be added:

Aliases

Add a section that is called aliases, and add as many aliases as you want. For example:

## aliases
# Byobu
alias byobu='ml use /home/biancoe/scratch/my_eb_apps/modules/all; ml purge; ml byobu; byobu'

# ls
alias la='ls -lah'alias lt='ls -lrth'

# Slurmalias interactive='salloc --time 1:00:00 srun --pty bash'
alias sq='squeue -o "%.18i %.9P %.5Q %.8j %.8u %.8T %.10M %.11l %.6D %.4C %.6b %.20S %.20R %.8q"'
alias squ='squeue -o "%.18i %.9P %.5Q %.8j %.8u %.8T %.10M %.11l %.6D %.4C %.6b %.20S %.20R %.8q" -u $USER'

Tokens. With caution!

It is not recommended, as stated before, but you can add your access token as variables that are exported when you enter your environment. 

  • Be sure your ~/.bashrc cannot be read by other users or applications (set permission to 600);
  • Renew your tokens when necessary or instructed to do so,
  • Consider doing unsets after running the relevant commands.

History

Bash keeps 500 commands in the history list, by default. This value is usually not enough for people interacting with the shell on a regular basis. Having the ability to search the history for a previous command saves time and reduces mistakes. 

You can change this value by updating the HISTSIZE variable. Similarly, the default size of the bash history file is 500. This is the maximum number of entries that are contained in the history file.

export HISTFILESIZE=5000
export HISTSIZE=5000

When you search for previous commands in your history, you may come across several duplicated lines, or commands that start with a space. You can avoid this by exporting HISTCONTROL in your ~/.bashrc:

export HISTCONTROL=erasedups:ignoredups:ignorespace

Prompt format

With the PS1 environment variable you can control the prompt format (i.e., adding colors or showing hostname or current directory in the prompt). This is a good example of  ~/.bashrc use, it is something that configures the shell itself, not unrelated applications.

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

Disable Colors

When you list the content of a folder in a Linux system, you will probably see a list of files with different colors. The color of each file indicates the type of file and its permissions. This means that the system needs to check the nature of the file, introducing unnecessary overhead for the cluster file system, especially when the number of files is extraordinarily large. Consider unsetting the variable LS_COLORS:

unset LS_COLORS

Language variable

You can add to your ~/.bashrc the UTF unicode you are using:

export LANG="en_US.UTF-8"

Connection timeout

Some hosts can be configured to auto-logout users after a set time of inactivity. This can kill long processes that are not detached using specific tools like tmux, screen, or nohup. The period can be specified in the TMOUT variable in seconds. Adding unset TMOUT in ~/.bashrc disables the auto-logout in those cases.

And always remember: your ~/.bashrc is loaded in order. In the event of a conflict, the last line value will win, and what you have set before might be unset at the end of the file. 

Keep it as clean as possible. 

Acknowledgements: We would like to gratefully and sincerely thank the valuable contribution of Danilo Gonzalez, Ph. D; Jordi Blasco; Maicon Faria, Ph. D for the discussion and contributions to this post. 

Authors:  Iván Díaz, Ph. D; Erica Bianco, Ph. D


Related Articles

Jordi Blasco

Enabling Abaqus Checkpointing & Restart with Slurm Workload Manager

How to enable this feature can be used to minimise the impact of a hardware issue by restarting the job from the last checkpoint.

Jordi Blasco

How to use the Slurm simulator as a development and testing environment

The Slurm simulator represents an extremely powerful. Benefits of adopting that technology, and how you can use it for your daily operations.

Elisabeth Ortega, Ph. D

Improving efficiency in HPC clusters using monitoring tools

A n efficient HPC cluster is one that has resources ready to be used on demand. Users are happy when they get access to the resources they need immediately.

Visit our forum

One of the main goals of this project is to motivate new initiatives and collaborations in the HPC field. Visit our forum to share your knowledge and discuss with other HPC experts!

About us

HPCKP (High-Performance Computing Knowledge Portal) is an Open Knowledge project focused on technology transfer and knowledge sharing in the HPC, AI and Quantum Science fields.

Promo HPCNow