Human Readable   

 

     
   
     

A Slackware Desktop Enhancement Guide

Harmonizing the Bash Startup Scripts

© Copyright Darrell Anderson.

Slackware is not packaged with all of the bash startup scripts. Not even an empty collection of scripts that could guide new GNU/Linux users toward understanding how these scripts function. This is unfortunate but easily remedied.

The bash startup scripts provide a user’s shell environment. For many people that shell will be bash, but there are other shells as well. For those people using bash, there are a handful of script files that can be used to customize a user’s shell environment. Some people might think that because they use a GUI they need not bother, but even with that environment, users occasionally need to open an xterm console.

The bash man page provides the details for how these scripts function and interact. In Slackware only one of those files exists: /etc/profile. The remaining files must be created and customized by users. The complete collection of those files include:

/etc/profile: generally used for system-wide initialization and containing global/system environment variables and startup programs.

/etc/bashrc: generally used for system-wide aliases and functions, and some modifications to /etc/profile.

~/.bash_profile: generally used to provide specific support related to logging in, and for local/personal environment variables and startup programs called only during the login process.

~/.bash_login: can be used in addition to .bash_profile if the file exists, but generally used as a substitute for .bash_profile.

~/.profile: can be used in addition to .bash_profile if the file exists, but generally used as a substitute for .bash_profile.

~/.bashrc: generally used for anything local/personal and peculiar to the user’s shell environment.

~/.bash_logout: generally used to provide specific support related to logging out.

Some users might be unfamiliar with the term sourcing. This term means that a script uses another file as though that file was written directly into the parent script. The convention used to source a file is the command source or using a dot convention. Either of the following conventions is acceptable:

. /etc/bashrc

source /etc/bashrc

In this example both lines will source the /etc/bashrc file into a script. Many people use the dot convention, but remember that for new users the dot convention probably means nothing and if using a text editor that displays tabs as dots, could lead to additional confusion. Using the source command is more friendly for new users. One caveat to remember about sourcing other files is that the file must be sourced before the information in that file is needed. Usually, therefore, all files that are sourced in a script are sourced at the beginning of the script.

As explained in the bash man page, the login command programmatically sources /etc/profile and then only ~/.bash_profile, or ~/.bash_login, or ~/.profile, and in that specific search order.

The logout command programmatically sources ~/.bash_logout only.

Peculiar to the way in which bash sources these files is that /etc/bashrc is never sourced directly by bash. Generally, the user’s ~/.bashrc file is sourced by bash only during non-login terminal sessions. Getting all of these files to work together seamlessly requires a wee bit of thought. Here is a basic plan:

  1. Bash always sources /etc/profile during the login and all environment variables exported in that process remain active throughout the login session.
  2. Use ~/.bash_profile to source ~/.bashrc.
  3. Use ~/.bashrc to source /etc/bashrc.
  4. Use ~/.bash_logout to execute specific logout tasks.

With this basic info, all that is needed is to create the scripts, login, and test. The bash startup scripts do not need to be executable (chmod +x) because they are sourced by bash. However, be sure to chmod +x any other script called from within these scripts that need executable permissions.

To run a program only when a specific user logs in, place that program in ~/.bash_profile. To run a program when anybody logs in, you could place the command in /etc/profile. However, rather than muck with the original scripts, consider creating a separate script and place that script in /etc/profile.d. All scripts stored in that directory are run from within /etc/profile. Name the script anything you want, but ensure the script uses an sh extension. For comparison sake, I use such a script named local.sh.

For those who prefer to copy and paste, here are examples that might prove useful:

/etc/bashrc

~/.bash_profile

~/.bashrc

~/.bash_logout

/usr/local/bin/bash_login

/usr/local/bin/bash_logout

Additional support files:

/etc/functions-colors

/etc/functions-system

/etc/profile.d/local.sh

After revising and testing these files to personal taste, be sure to copy the three user files to the /etc/skel directory where they then will be available to new users when creating new accounts. Additional information about the /etc/skel directory is available here.

Finis.

Table of Contents