logo

Bash

What is Bash?

Bash (Bourne Again Shell) is (almost) universally available on Linux, macOS, and even Windows.

In many Linux distros, bash is the default shell, and /bin/sh is symlinked to /bin/bash.

Bash is not standardized, but there's only one implementation of bash (i.e. bash is defined by its implementation).

The official manual: http://www.gnu.org/software/bash/manual/bash.html

Check Bash Version

  • bash --version
  • echo "${BASH_VERSION}"
  • Ctrl+x Ctrl+v

Image Not Found

Error

dyld: Library not loaded: /usr/local/opt/readline/lib/libreadline.6.dylib
  Referenced from: /usr/local/bin/bash
  Reason: image not found
Trace/BPT trap: 5

Solution

Reinstall bash

$ brew upgrade bash

Style Guide

Use "$foo", or "${foo}", not $foo

Different from the POSIX shell

  • [[ condition ]] does not work; use [ condition ]
  • Arrays do not work; use IFS
  • Local variables do not work; use a subshell

Config

Bash uses two startup configuration files .bashrc and .bash_profile to initialize and customize the shell.

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

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

such as alias rm = "rm -i" and export more variables (such as export PAGER=less).

Your new settings may be added to either .bashrc or .bash_profile. Basically, .bashrc and .bash_profile are run under different conditions. If you ssh <server> <command>, the shell will run ~/.bashrc; if you ssh <server> (with no <command>) or slogin <server>, the shell will run ~/.bash_profile. Certain types of commands belong to .bash_profile and are best kept out of .bashrc.

'export' your variables in order to have them show in env

Add .bashrc to ~/.bash_profile

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

Change Terminal Color

export CLICOLOR=1
export LSCOLORS=ExFxCxDxBxegedabagacad

Always Search Current Folder First

Add to ~/.bashrc or ~/.bash_profile

export PATH=`pwd`:$PATH

Style Your Prompt

  • PS1: the primary prompt display, where you set special characters or important information.
  • PS2: the secondary prompt string, usually set as a divider between the prompt display and the text entry. It is also used to display when a long command is broken into sections with the \ sign.
  • PS3: for the select command.
  • PS4: for running a shell script in debug mode.

Add to ~/.bashrc or ~/.bash_profile

function prompt {
  local    NO_COLOR="\e[0m"
  local       BLACK="\[\033[0;30m\]"
  local         RED="\[\033[0;31m\]"
  local   LIGHT_RED="\[\033[1;31m\]"
  local       GREEN="\[\033[0;32m\]"
  local LIGHT_GREEN="\[\033[1;32m\]"
  local      YELLOW="\[\033[0;33m\]"
  local        BLUE="\[\033[0;34m\]"
  local     MAGENTA="\[\033[0;35m\]"
  local        CYAN="\[\033[0;36m\]"
  local       WHITE="\[\033[1;37m\]"
  local  LIGHT_GRAY="\[\033[0;37m\]"

  PS1="\n${RED}\u@\h: ${GREEN}\w ${NO_COLOR}\n\$ "
}
prompt

It should look like:

<user(red)>@<host(green)>: <working folder>
$

Color Scheme

DULL=0
BRIGHT=1

FG_BLACK=30
FG_RED=31
FG_GREEN=32
FG_YELLOW=33
FG_BLUE=34
FG_VIOLET=35
FG_CYAN=36
FG_WHITE=37

FG_NULL=00

BG_BLACK=40
BG_RED=41
BG_GREEN=42
BG_YELLOW=43
BG_BLUE=44
BG_VIOLET=45
BG_CYAN=46
BG_WHITE=47

BG_NULL=00

##
# ANSI Escape Commands
##
ESC="\033"
NORMAL="\[$ESC[m\]"
RESET="\[$ESC[${DULL};${FG_WHITE};${BG_NULL}m\]"

##
# Shortcuts for Colored Text ( Bright and FG Only )
##

# DULL TEXT

BLACK="\[$ESC[${DULL};${FG_BLACK}m\]"
RED="\[$ESC[${DULL};${FG_RED}m\]"
GREEN="\[$ESC[${DULL};${FG_GREEN}m\]"
YELLOW="\[$ESC[${DULL};${FG_YELLOW}m\]"
BLUE="\[$ESC[${DULL};${FG_BLUE}m\]"
VIOLET="\[$ESC[${DULL};${FG_VIOLET}m\]"
CYAN="\[$ESC[${DULL};${FG_CYAN}m\]"
WHITE="\[$ESC[${DULL};${FG_WHITE}m\]"

# BRIGHT TEXT
BRIGHT_BLACK="\[$ESC[${BRIGHT};${FG_BLACK}m\]"
BRIGHT_RED="\[$ESC[${BRIGHT};${FG_RED}m\]"
BRIGHT_GREEN="\[$ESC[${BRIGHT};${FG_GREEN}m\]"
BRIGHT_YELLOW="\[$ESC[${BRIGHT};${FG_YELLOW}m\]"
BRIGHT_BLUE="\[$ESC[${BRIGHT};${FG_BLUE}m\]"
BRIGHT_VIOLET="\[$ESC[${BRIGHT};${FG_VIOLET}m\]"
BRIGHT_CYAN="\[$ESC[${BRIGHT};${FG_CYAN}m\]"
BRIGHT_WHITE="\[$ESC[${BRIGHT};${FG_WHITE}m\]"

# REV TEXT as an example
REV_CYAN="\[$ESC[${DULL};${BG_WHITE};${BG_CYAN}m\]"
REV_RED="\[$ESC[${DULL};${FG_YELLOW}; ${BG_RED}m\]"

PROMPT_COMMAND='export ERR=$?'

Show Current Git Branch

Add to ~/.bashrc or ~/.bash_profile

function parse_git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

Then change the last line in prompt function to

PS1="\n${RED}\u@\h:${BLUE}\$(parse_git_branch) ${GREEN}\w ${BLACK}\n\$ "

path_helper

The path_helper utility reads the contents of the files in the directories /etc/paths.d and /etc/manpaths.d and appends their contents to the PATH and MANPATH environment variables respectively. Files in these directories should contain one path element per line.

/etc/profile

# System-wide .profile for sh(1)

if [ -x /usr/libexec/path_helper ]; then
    eval `/usr/libexec/path_helper -s`
fi

if [ "${BASH-no}" != "no" ]; then
    [ -r /etc/bashrc ] && . /etc/bashrc
fi