Bash



Snippets

  • Page:
    Args — Manipulating the input command line arguments
  • Page:
    Colours — How to support colours in a bash script
  • Page:
    Conditionals — Various testing on conditional events or variables (exit test, variable defaults).
  • Page:
    Environment — Introspecting and manipulating the shell environment
  • Page:
    Substitution — Search, find and replace from the command line
  • Page:
    User Interaction — Interacting with the user for, e.g. input.

Configuration

  • .profile - general shell commands for use with any shell (i.e. PATH etc)
  • .bash_profile - super simple, just source .profile and .bashrc if they exist
  • .bashrc - shell specific commands (command line prompt, aliases, etc)


 Examples...
# .profile
export PATH="${HOME}/.local/bin:${PATH}

# .bash_profile
if [ -f ~/.profile ]; then
   source ~/.profile
fi

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

# .bashrc
function _update_ps1() {
    PS1=$(powerline-shell $?)
}
if [[ $TERM != linux && ! $PROMPT_COMMAND =~ _update_ps1 ]]; then
    PROMPT_COMMAND="_update_ps1; $PROMPT_COMMAND"
fi
if [ -f ~/.bash_aliases ]; then
   source ~/.bash_aliases
fi

MacOSX

  • ANSI color codes - always use printf, not echo
  • Terminal → Preferences → General → Command → /bin/bash
    • NB: Terminal will always run a login shell