Skip to content

How to Use Bash Startup Scripts to Customize Your Shell Environment

November 17, 2022
Bash Startup Script

This blog post will teach you how to use Bash Startup Scripts to customize your shell environment and make your work more efficient.

Bash Startup scripts

Profile

  • Stored in /etc/profile global) and ~/.bash_profile (user)
  • Run for login shells only
  • Used for
    • Setting evironment variables
    • Running commands (eg mail-checker script)

The /etc/profile shell script is the first startup script run when a login shell is started. It only runs for login shells; non-login shells do not invoke this script.

The script will set a series of variables including PATH, USER, LOGNAME, MAIL, HOSTNAME, HISTSIZE, and INPUTRC.

It will also run the scripts found in the /etc/profile.d directory.

Login shells first call /etc/profile, which calls /etc/profile.d. Then, the file ~/.bash_profile is called. This file, in turn, calls ~/.bashrc, which calls /etc/bashrc. Each script in turn can undo changes made in previously called scripts. For example, the PATH variable is set in the /etc/profile script, but is then modified in the ~/.bash_profile script.

bashrc

  • Stored in /etc/bashrc (grobal) and ~/.bashrc (user)
  • Run for all bash shells
  • Used for
    • Setting local variables
    • Defining aliases

Non-login shells reference some of the same files, but in a different order. Non-login shells first call ~/.bashrc. This calls /etc/bashrc, which calls /etc/profile.d. Note that the /etc/bashrc file only calls /etc/profile.d for non-login shells; for login shells, the previously called /etc/profile calls the /etc/profile.d scripts.

Typical sorts of commands placed in startup scripts include:

local variable settings, particularly PS1

environment variable settings, such as PATH or LESS

aliases, or perhaps unaliases to remove undesired aliases, set globally in earlier scripts a umask, to be discussed in next article.

Sourcing files

  • Changes to profile and bashrc files need to be sourced
  • Two methods:
    • .scriptname
    • source scriptname
  • Shell scripts can source other files

Changes made to profile and bashrc files need to be sourced. This will execute the file and read it into the running shell. For instance, if you make a change to /etc/bashrc, opening a new terminal will source this file and activate your changes. However, the original terminal where you made those changes still has the old settings. From the original terminal, run one of the following:

[root@amar-pc ~]# . /etc/bashrc

(OR)

 [root@amar-pc ~]# source /etc/bashrc

Bash Exit Tasks

  • Stored in ~/.bash_logout (user)
  • Run when a login shell exits
  • Used for
    • Creating automatic backups
    • Cleaning out temporary files

There are certain tasks that a user may wish to be performed when they log out of a system. Tasks of this type could include deleting temporary files as well as creating backups of files which the user is working on.

To accomplish this, all commands placed in .bash_logout, which can be found in a user’s home directory, will be executed when a login shell exits. For example, a user may login through a display managed graphical login, work on some files and then log out. This .bash_logout script is then automatically called when the X session is ended, for example to run a command that backs up the users’ data or cleans up old files.