☼
⌃
☰

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

Quick Reference

Redirection

SHELL
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

SHELL
command1 | command2           # pipe stdout to next command
command1 | tee file           # pipe and also write to file
command1 | command2 | command3  # chain

Command Substitution

SHELL
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

SHELL
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