bash reference
startup files (login vs non-login)
| File | Used when |
/etc/profile | System-wide login shell |
~/.bash_profile or ~/.profile | User login shell |
~/.bashrc | Interactive non-login (and usually sourced from .bash_profile) |
~/.bash_logout | On logout (login shells) |
snippets for ~/.bashrc
# Prompt with git branch
parse_git_branch() { git rev-parse --abbrev-ref HEAD 2>/dev/null; }
export PS1='\\u@\\h \\W$(gb=$(parse_git_branch); [ -n \"$gb\" ] && echo \" (\$gb)\")$ '
# Safer defaults
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias ll='ls -alF'
alias gs='git status -sb'
# History tuning
export HISTCONTROL=ignoredups:erasedups
export HISTSIZE=5000
export HISTFILESIZE=10000
shopt -s histappend cmdhist checkwinsize
# Color grep and ls
export CLICOLOR=1
alias grep='grep --color=auto'
# Add user bin path
export PATH=\"$HOME/bin:$PATH\"
aliases (quick rules)
auto-source bashrc from profile
# ~/.bash_profile
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
auto-attach tmux on ssh
# place in ~/.bashrc (after non-interactive guard if you have one)
if [ -n \"$SSH_CONNECTION\" ] && [ -z \"$TMUX\" ]; then
tmux new -A -s ssh
exit
fi
completion & prompt extras
# Enable programmable completion (Debian/Ubuntu)
if [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
# Timer for last command
trap 'LAST=$SECONDS' DEBUG
PROMPT_COMMAND='RT=$SECONDS; DUR=$((RT-LAST)); history -a; printf \"\"'
PS1='\\u@\\h \\W [${DUR}s]$ '
quick functions
# mkcd: make dir then cd
mkcd() { mkdir -p \"$1\" && cd \"$1\"; }
# extract various archives
x() {
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 \"don't know how to extract '$1'\" ;;
esac
}
Return to Home