Bash Shell

General Information (for the absolute beginner)

When logging in to a Bash shell, the machine finds and executes commands in /etc/profile, and will then search for .bash_profile, .bash_login, and .profile (in that order) in the login user's home directory. When exiting / loggin out, bash reads and executes ~/.bash_logout (if it exists).

(Will add more here...)

Tips and Tricks

I like to change the color, and structure, of my Bash prompt to make a more clear distinction between the area that I am typing and the prompt. A typical Bash prompt looks like user@machine:present working directory$ (see: image). A stylized prompt could look like this. You can change the structure (where to put the machine name, user name, path) and the color of each element. My .prompt file defines all of the colors, and shows and example structure:

COL_BLACK=$'\[\033[30m\]'
COL_RED=$'\[\033[31m\]'
COL_GREEN=$'\[\033[32m\]'
COL_YELLOW_ORANGE=$'\[\033[33m\]'
COL_BLUE=$'\[\033[34m\]'
COL_MAGENTA=$'\[\033[35m\]'
COL_CYAN=$'\[\033[36m\]'
COL_NORM=$'\[\033[39m\]'
PMPT_BOLD=$'\[\033[1m\]'
PMPT_NORM=$'\[\033[0m\]'

# See: http://networking.ringofsaturn.com/Unix/Bash-prompts.php
# for a longer list of colours.

PS1="${PMPT_BOLD}${COL_CYAN}\h${COL_YELLOW_ORANGE} [\w] %${PMPT_NORM} "

The line PS1="${PMPT_BOLD}${COL_CYAN}\h${COL_YELLOW_ORANGE} [\w] %${PMPT_NORM} " is what defines the colors, and structure, of the displayed prompt. Breaking it down, it sets the font weight to be bold (${PMPT_BOLD}), the color of the host name to be cyan (${COL_CYAN}\h), the color of the current working directory to be yellow-orange, and surrounded by brackets (${COL_YELLOW_ORANGE} [\w]), and then at the end returns the font weight back to normal (${PMPT_NORM}).

"Style"

I try to break up the content in my .bash_profile (for purely organizational reasons) into aliases, environment variables, and prompt manipulation. I store aliases in the file .alias, environment variables in .env and prompt manipulation in .prompt. In fact, my ~/.bash_profile really just looks like:

if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

source .env
source .alias
source .prompt

- Adrian

Attachments