Photo Dave Ashutosh un unsplash

When you start your UNIX/Linux session, some magic happens at startup. Many problems find their solution in this startup magic. We will see, here with bash shell what you can do to reduce potential problems to a minimum. These concepts can easily be translated to other shells.

KISS rule : Keep It Simple and Stupid!

While you can largely pimp up your UNIX experiences, the most basic rule is to keep all the stuff optional.

Things to avoid

By default, the intial situation should be close to absolute nothing:

No verbose startup

An echo "Welcome home, master" writes on the standard output. This can jam some ssh command from elsewhere.

No automatic customization

With an implicit change of local format (LOCALE, you can break the process of other people who provide you tools , i.e. a date parser. Ok they could have done it more robust, but why should it make you loose more time than necessary?

Similarly, with an implicit change of keyboard layout, you can be stuck the next time you connect to this account from an other terminal.

Use environment variable just in time

Keep as few as possible Environments Variables setup. These are adding a lot of statefulness to your situation, making you exposed to history related bugs.

Keep your profile to one single file

If your profile setup is scattered over several files, finding a bug there will take more time.

No module load by default

The modules are a common technique for administrators to help you get the in a reproducible environment.

By essence a module load changes your environment. Like environement variables, this should only happen at your explicit request.

Extend commands, never replace

The functions and aliases are good ways to create new commands. However, never replace a pre-existing command, because scripts made by others could break.

See this example:

alias git ="cd $HOME/GITLAB"

All scripts using git will be non-operationnal, but the git command never returns an error. Hide this somehere, and you will drive your computer support nuts.

But I do want to customize my terminal!

You can customize your terminal safely with functions. A function provides you a convenient way to explicitly ask for a personal setup.

# Basic function
function print_something {
echo Hello I am a function

function print_something_arg {
 echo Hello $1
Hello I am a function

>print_something_arg Mars
Hello Mars

You can store your personal customization in your profile, without the initial hidden perturbations, like this:

function at_office {
    echo "Setup session for Office work"
    export LANG=en_US.UTF-8
    export SPEDCIALPATH=$HOME/local/foo/bar
    module load heavyduty/1.2.0

Now, when you log in, use the at_office command to load your preferences.

A safe .bash_profile example

Here follows a simple bash profile. Note how we use docstrings (# comment) to explain why lines are here.

# generic commands
alias sc='source ~/.bash_profile' # reboot this file
alias priv='vi ~/.bash_profile' # edit this file
alias cda="cd /archive/cfd/$USER" #jump to archive dir

# Return a variable splitted on : separator
# e.g. echo_split $PATH 
function echo_split  {
    tr ':' '\n' <<< $1

# My personal customization
function customize {
    echo "Setup session for Office work"
    export PS1="\[\033[38;5;59m\]\u@\h\[$(tput sgr0)\]\[\033[38;5;15m\] \[$(tput sgr0)\]\[\033[38;5;30m\]\w\[$(tput sgr0)\]\[\033[38;5;15m\] \[$(tput sgr0)\]\[\033[38;5;80m\]>\[$(tput sgr0)\]" # pretty prompt
    export TERM=xterm-256color # colored terminal
    export LANG=en_US.UTF-8 # us format
    export LC_NUMERIC="en_US.UTF-8"

# loading the usual working python virtual environment
function venv_default {
    echo "Default virtual environment"
    source /archive/cfd/dauptain/PythonVenvs/dev_opentea/bin/activate

# start a NICE session
function nice_start {
    dcv create-session dauptain
    dcv list-sessions
    echo "Go to webpage"

With this profile, the startup is almost history free, and you can rely on the functions:

>echo_split $PATH

Note that unless you are a memory freak, your usage will eventually shrink to less than 6 personal frequent functions.

Like this post? Share on: TwitterFacebookEmail

Antoine Dauptain is a research scientist focused on computer science and engineering topics for HPC.

Keep Reading





Stay in Touch