UNIX Login Scripts

Login scripts are used to set your environment when logging in or opening a terminal on a unix or linux machine.

For information specific to CESR login scripts, please see https://cesrwww.lepp.cornell.edu/wiki/CESR/LoginScripts .

Output

Your UNIX login scripts should not produce output unless you are in an interactive shell. This confuses (and breaks) things like less, rcp, scp, rsh, nx, etc. The fix is to make sure your login scripts don't print anything (or, they only print in environments where it is sure to be safe--for example, by making sure you are in an interactive shell).

Output from .cshrc or .bashrc will confuse many useful things, like rcp/scp/rsh and non-interactive uses of ssh, so keeping these scripts quiet in non-interactive shells is generally a good idea.

Bash

~/.bash_profile is executed for login shells. In other words, when you login either graphically (GNOME, IDE, IceWM, etc.) or using ssh, ~.bash_profile is executed to configure your shell before the initial command prompt.

~/.bashrc is executed for interactive non-login shells. In other words, if you’ve already logged into your machine and open a new terminal window or type = /bin/bash=, ~.bashrc is executed before the window command prompt.

So anything you only want to see or execute on login should be in ~/.bash_profile. As you'll see in the default login scripts below, we recommend putting source ~/.bashrc in ~/.bash_profile to make sure your environment is configured appropriately for both login and interactive shells.

If your bash login scripts need to produce output, make sure they only print in environments where it is sure to be safe--for example, by making all echoes conditional on $PS1 being set (which should only be true of interactive shells)). For example:
   
if [ "$PS1" ]; then
    ...
fi

Finally, please be sure to not setup your environment to automatically source packages in /opt/rh. This breaks many things, including your window manager.

Defaults

.bash_profile

For reference, see:
/nfs/user/default/home/.bash_profile

Contents:
# .bash_profile

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

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
unset USERNAME

.bashrc

For reference, see:
/nfs/user/default/home/.bashrc

Contents:
# .bashrc

# User specific aliases and functions

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

if [ "$PS1" ]; then
 # This section will only be executed by interactive sessions.
 # Add "echo" commands and other things that print to the
 # terminal here.
 echo -n ""
 # Prevent dbus-daemon from hanging logout from ssh sessions
 if [ -z "$DBUS_SESSION_BUS_ADDRESS" ] && [ -n "$DISPLAY" ]; then
  ## if not found, launch a new one detached from stdin
  eval $(dbus-launch --sh-syntax --exit-with-session </dev/null)
 fi
fi

TCSH

Make sure your .cshrc doesn't print anything except in environments where it is sure to be safe--for example, by making all echoes conditional on $prompt being set (which should only be true of interactive shells).

Likewise, any stty command should be in ~/.login (instead of ~/.cshrc) and should only execute in interactive shells. Another example:
if [ -t 0 ]; then
   echo interactive
   stty erase ^H
else
   echo non-interactive
fi

LESS

Red Hat Linux sets the LESSOPEN environment variable to point to the shell script /usr/bin/lesspipe.sh to handle things like automatically decompressing files that end in .gz and doing a 'tar tvvf' on tar files to show you a list of the contests. Your shell gets invoked as part of forking the pipe to run the script; tcsh always runs .cshrc at startup, and the echo output confuses less (which is expecting to only see output from the $LESSOPEN script). You can

unsetenv LESSOPEN

in your .cshrc -- this will fix the problem, at the expense of breaking the automatic features. A better fix is to make sure your login scripts only produce output when it is safe to do so, as described above.

Back to: ComputingFAQ

This topic: Computing > UnixLoginScripts
Topic revision: 06 May 2019, admindo-dab66
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding CLASSE Wiki? Send feedback