Bash Shell Guide
Bash (Bourne Again Shell) is the default shell on most Linux distributions and macOS. It’s a powerful, feature-rich shell that’s perfect for both interactive use and scripting.
Key Features
Section titled “Key Features”Interactive Use
- Command history with arrow keys
- Tab completion for commands, files, and arguments
- Job control (foreground/background processes)
- Aliases and functions
Scripting
- Full programming language features
- String manipulation
- Arrays
- Conditionals and loops
Installation
Section titled “Installation”macOS (default, but outdated)
# Install latest via Homebrewbrew install bash# Add to list of shellssudo bash -c 'echo /opt/homebrew/bin/bash >> /etc/shells'# Set as defaultchsh -s /opt/homebrew/bin/bashUbuntu/Debian
# Already installed! Just set as defaultchsh -s /usr/bin/bashConfiguration Files
Section titled “Configuration Files”| File | Purpose |
|---|---|
~/.bashrc |
Run for interactive non-login shells |
~/.bash_profile |
Run for login shells (macOS) |
~/.bash_login |
Run if .bash_profile not found |
~/.profile |
Run for login shells (Ubuntu) |
/etc/bash.bashrc |
System-wide config |
Tip: Put interactive configs in ~/.bashrc and login configs in ~/.profile
Common Aliases
Section titled “Common Aliases”# Shortcutsalias ll='ls -lah'alias la='ls -A'alias l='ls -CF'
# Safetyalias rm='rm -i'alias cp='cp -i'alias mv='mv -i'
# Git shortcutsalias gs='git status'alias ga='git add'alias gc='git commit'alias gp='git push'alias gl='git log --oneline -10'
# Navigationalias ..='cd ..'alias ...='cd ../..'alias ....='cd ../../..'Functions
Section titled “Functions”# Function with argumentsfunction mkcd() { mkdir -p "$1" && cd "$1"}
# Usage: mkcd newproject
# Git functionsfunction gco() { git checkout "$1"}
# Extract archivesextract() { if [ -f "$1" ] ; then case "$1" in *.tar.bz2) tar xjf "$1" ;; *.tar.gz) tar xzf "$1" ;; *.bz2) bunzip2 "$1" ;; *.rar) unrar x "$1" ;; *.gz) gunzip "$1" ;; *.tar) tar xf "$1" ;; *.tbz2) tar xjf "$1" ;; *.tgz) tar xzf "$1" ;; *.zip) unzip "$1" ;; *.Z) uncompress "$1" ;; *.7z) 7z x "$1" ;; *) echo "'$1' cannot be extracted" ;; esac else echo "'$1' is not a valid file" fi}Prompt Customization
Section titled “Prompt Customization”Basic PS1 Example
Section titled “Basic PS1 Example”# Add to .bashrcexport PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
# Color codes:# \033[0;30m - Black# \033[0;31m - Red# \033[0;32m - Green# \033[0;33m - Yellow# \033[0;34m - Blue# \033[0;35m - Purple# \033[0;36m - Cyan# \033[0;37m - WhiteGit Branch in Prompt
Section titled “Git Branch in Prompt”parse_git_branch() { git branch 2>/dev/null | grep '*' | sed 's/* //'}
PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ 'Environment Variables
Section titled “Environment Variables”# Add to PATHexport PATH="$HOME/bin:$PATH"
# Editorexport EDITOR=vimexport VISUAL=vim
# Historyexport HISTSIZE=10000export HISTFILESIZE=20000export HISTCONTROL=ignoredups:erasedupsUseful Shortcuts
Section titled “Useful Shortcuts”| Shortcut | Action |
|---|---|
Ctrl+A |
Move to beginning of line |
Ctrl+E |
Move to end of line |
Ctrl+U |
Clear line before cursor |
Ctrl+K |
Clear line after cursor |
Ctrl+R |
Search history |
Ctrl+L |
Clear screen |
Ctrl+C |
Cancel current command |
Ctrl+Z |
Suspend/background process |
!! |
Repeat last command |
!$ |
Last argument of previous command |
Shell Options
Section titled “Shell Options”# Useful shopt optionsshopt -s histappend # Append to historyshopt -s checkwinsize # Update window sizeshopt -s globstar # ** for recursive pathsshopt -s nocaseglob # Case-insensitive globbing
# Add to .bashrc to make permanentConditionals
Section titled “Conditionals”# Basic ifif [ -f "$file" ]; then echo "File exists"fi
# If-elseif [ $# -eq 0 ]; then echo "No arguments"else echo "Arguments: $@"fi
# String comparisonsif [ "$var" = "hello" ]; then echo "Hello!"fi
# Numeric comparisonsif [ $num -gt 10 ]; then echo "Greater than 10"fi# For loop over filesfor file in *.txt; do echo "Processing $file"done
# For loop over command outputfor i in $(seq 1 5); do echo "Number $i"done
# While loopwhile read line; do echo "$line"done < file.txtTips & Tricks
Section titled “Tips & Tricks”Colorized output
Section titled “Colorized output”# Redecho -e "\033[0;31mThis is red\033[0m"
# Greenecho -e "\033[0;32mThis is green\033[0m"Check command success
Section titled “Check command success”cd /some/directory && echo "Success" || echo "Failed"Default value
Section titled “Default value”# Use default if variable is emptyecho "${VAR:-default}"