Bash
Table of Contents
Bourne Again SHell — the default interactive shell and scripting language on most Linux systems. This note is an index into the sub-notes covering specific command categories. For scripting detail see Bash Scripting.
Related: Bash File Manipulation Commands Bash Navigation Commands Bash Process Management Commands Bash Scripting Bash Text Processing Commands Regular Expressions
Command Categories
- Bash Navigation Commands —
cd,ls,pwd,find - Bash File Manipulation Commands —
cp,mv,rm,chmod,chown - Bash Text Processing Commands —
grep,awk,sed,cut,sort - Bash Process Management Commands —
ps,kill,jobs,bg,fg - Bash Scripting — variables, conditionals, loops, functions
Quick Reference
Redirection
command > file # stdout to file (overwrite) command >> file # stdout to file (append) command 2> error.log # stderr to file command 2>&1 # redirect stderr to stdout command > /dev/null 2>&1 # suppress all output command < file # stdin from file
Pipes
command1 | command2 # pipe stdout to next command command1 | tee file # pipe and also write to file command1 | command2 | command3 # chain
Command Substitution
result=$(command) # capture output as variable echo "Hostname: $(hostname)" files=$(find . -name "*.py")
Useful Shortcuts
| Shortcut | Action |
|---|---|
Ctrl+C |
Kill running process |
Ctrl+Z |
Suspend process to background |
Ctrl+R |
Reverse search history |
Ctrl+L |
Clear screen |
!! |
Repeat last command |
!$ |
Last argument of previous command |
Alt+. |
Insert last argument of previous command |
Debugging Scripts
bash -x script.sh # trace execution (print each command) set -x # enable trace inside script set -e # exit on any error set -u # treat unset variables as errors set -euo pipefail # common safe combination