The Bash Shell Startup Files

#### Gaming Linux® From Scratch - Version 13.0

### Chapter 3. Preparing for GLFS

- Prev

  Rationale for Packages

- Next

  Connecting to the Internet

- Up
- Home

# The Bash Shell Startup Files

Bash, or the shell, can source a file at startup that applies for all users, which can be very helpful for many different purposes. This can involve setting many useful variables to adding new functionality to the shell, usually for convenience. This section is therefore recommended and setting up packages in this book will rely on this section and the created files. There is little downside to following this section unless you are not able to copy and paste the following commands, in which case there will be a lot of typing involved. Doing this in a chroot is recommended before booting back into your LFS system.

### Note

Most of the instructions below are used to create files located in the `/etc` directory structure which requires you to execute the commands as the `root` user. If you elect to create the files in user's home directories instead, you should run the commands as an unprivileged user.

### Note

There are more startup files that can be made. The ones here establish a basic setup while offering convenience. For more startup files, see BLFS Bash Startup Files.

## /etc/profile

Here is a base `/etc/profile`. This file starts by setting up some helper functions and some basic parameters. It specifies some bash history parameters and, for security purposes, disables keeping a permanent history file for the `root` user. It also sets a default user prompt. It then calls small, single purpose scripts in the `/etc/profile.d` directory to provide most of the initialization.

For more information on the escape sequences you can use for your prompt (i.e., the `PS1` environment variable) see info bash -- Node: Printing a Prompt.

    cat > /etc/profile << "EOF"
    # Begin /etc/profile
    # Written for Beyond Linux From Scratch
    # by James Robertson <jameswrobertson@earthlink.net>
    # modifications by Dagmar d'Surreal <rivyqntzne@pbzpnfg.arg>

    # System wide environment variables and startup programs.

    # System wide aliases and functions should go in /etc/bashrc.  Personal
    # environment variables and startup programs should go into
    # ~/.bash_profile.  Personal aliases and functions should go into
    # ~/.bashrc.

    # Functions to help us manage paths.  Second argument is the name of the
    # path variable to be modified (default: PATH)
    pathremove () {
            local IFS=':'
            local NEWPATH
            local DIR
            local PATHVARIABLE=${2:-PATH}
            for DIR in ${!PATHVARIABLE} ; do
                    if [ "$DIR" != "$1" ] ; then
                      NEWPATH=${NEWPATH:+$NEWPATH:}$DIR
                    fi
            done
            export $PATHVARIABLE="$NEWPATH"
    }

    pathprepend () {
            pathremove $1 $2
            local PATHVARIABLE=${2:-PATH}
            export $PATHVARIABLE="$1${!PATHVARIABLE:+:${!PATHVARIABLE}}"
    }

    pathappend () {
            pathremove $1 $2
            local PATHVARIABLE=${2:-PATH}
            export $PATHVARIABLE="${!PATHVARIABLE:+${!PATHVARIABLE}:}$1"
    }

    export -f pathremove pathprepend pathappend

    # Set the initial path
    export PATH=/usr/bin

    # Attempt to provide backward compatibility with LFS earlier than 11
    if [ ! -L /bin ]; then
            pathappend /bin
    fi

    if [ $EUID -eq 0 ] ; then
            pathappend /usr/sbin
            if [ ! -L /sbin ]; then
                    pathappend /sbin
            fi
            unset HISTFILE
    fi

    # Set up some environment variables.
    export HISTSIZE=1000
    export HISTIGNORE="&:[bf]g:exit"

    # Set some defaults for graphical systems
    export XDG_DATA_DIRS=${XDG_DATA_DIRS:-/usr/share}
    export XDG_CONFIG_DIRS=${XDG_CONFIG_DIRS:-/etc/xdg}
    export XDG_RUNTIME_DIR=${XDG_RUNTIME_DIR:-/tmp/xdg-$USER}

    for script in /etc/profile.d/*.sh ; do
            if [ -r $script ] ; then
                    . $script
            fi
    done

    unset script

    # End /etc/profile
    EOF

### The /etc/profile.d Directory

Now create the `/etc/profile.d` directory, where the individual initialization scripts are placed:

    install --directory --mode=0755 --owner=root --group=root /etc/profile.d

### /etc/profile.d/extrapaths.sh

This script adds some useful paths to the `PATH` and can be used to customize other PATH related environment variables (e.g. LD_LIBRARY_PATH, etc) that may be needed for all users.

    cat > /etc/profile.d/extrapaths.sh << "EOF"
    if [ -d /usr/local/lib/pkgconfig ] ; then
            pathappend /usr/local/lib/pkgconfig PKG_CONFIG_PATH
    fi
    if [ -d /usr/local/bin ]; then
            pathprepend /usr/local/bin
    fi
    if [ -d /usr/local/sbin -a $EUID -eq 0 ]; then
            pathprepend /usr/local/sbin
    fi

    if [ -d /usr/local/share ]; then
            pathprepend /usr/local/share XDG_DATA_DIRS
    fi

    # Set some defaults before other applications add to these paths.
    pathappend /usr/share/info INFOPATH
    EOF

### Note

The man program automatically deduces the search path for man pages by examining the content of the `PATH` variable, see manpath(5) for details. Setting the `MANPATH` variable may override the automatic deduction, so the BLFS editors do not recommend setting it. If you must set it for any reason, it's better to start its value with a colon (`:`), for example MANPATH=:/opt/somepkg/share/man:/opt/otherpkg/share/man so the paths listed in the `MANPATH` variable will be appended to the automatically deduced value instead of overriding it.

### /etc/profile.d/readline.sh

This script sets up the default `inputrc` configuration file. If the user does not have individual settings, it uses the global file.

    cat > /etc/profile.d/readline.sh << "EOF"
    # Set up the INPUTRC environment variable.
    if [ -z "$INPUTRC" -a ! -f "$HOME/.inputrc" ] ; then
            INPUTRC=/etc/inputrc
    fi
    export INPUTRC
    EOF

### /etc/profile.d/umask.sh

Setting the umask value is important for security. Here the default group write permissions are turned off for system users and when the user name and group name are not the same.

    cat > /etc/profile.d/umask.sh << "EOF"
    # By default, the umask should be set.
    if [ "$(id -gn)" = "$(id -un)" -a $EUID -gt 99 ] ; then
      umask 002
    else
      umask 022
    fi
    EOF

### /etc/profile.d/i18n.sh

This script sets an environment variable necessary for native language support. A full discussion on determining this variable can be found on the Configuring the System Locale page.

    cat > /etc/profile.d/i18n.sh << "EOF"
    # Set up i18n variables
    for i in $(locale); do
      unset ${i%=*}
    done

    if [[ "$TERM" = linux ]]; then
      export LANG=C.UTF-8
    else
      source /etc/locale.conf

      for i in $(locale); do
        key=${i%=*}
        if [[ -v $key ]]; then
          export $key
        fi
      done
    fi
    EOF

### /etc/profile.d/dircolors.sh

This script uses the `~/.dircolors` and `/etc/dircolors` files to control the colors of file names in a directory listing. They control colorized output of things like ls --color.

    cat > /etc/profile.d/dircolors.sh << "EOF"
    # Setup for /bin/ls and /bin/grep to support color
    if [ -f "/etc/dircolors" ] ; then
            eval $(dircolors -b /etc/dircolors)
    fi

    if [ -f "$HOME/.dircolors" ] ; then
            eval $(dircolors -b $HOME/.dircolors)
    fi
    EOF

Now create the `dircolors` data:

    dircolors -p > /etc/dircolors

### /etc/profile.d/aliases.sh

This script aliases some tools to pass `--color=auto` to use `dircolors` functionality which depends on the script in the last section.

    cat > /etc/profile.d/aliases.sh << "EOF"
    # Make ls, grep, and diff use --color=auto
    alias ls='ls --color=auto'
    alias grep='grep --color=auto'
    alias diff='diff --color=auto'
    EOF

### Sourcing All the Profile Scripts

Each login, the scripts above will be sourced and will establish environment variables and functions. However, doing a continuous run of this book, BLFS, and/or SLFS that started before creating the above scripts will lead to the scripts not being sourced automatically. They must be sourced manually first before the environment variables and functions are available to the current login session. You should make it a point to source the profile scripts after creating them.

Now source the main profile:

    source /etc/profile

- Prev

  Rationale for Packages

- Next

  Connecting to the Internet

- Up
- Home