Skip to content

Bash

Bash is the default shell for many (“most” may even be accurate) Linux distributions. The “Bourne Again SHell” was first released in 1989 by Brian Fox for UNIX-like operating systems, and is designed as an alternative for the older Bourne shell, sh. Bash is both a shell and a programming language. Scripts written in Bash are called “shell scripts,” and facilitate automation by chaining commands and handling exit codes. Read more about the history of Bash on Wikipedia.

Bash Cheat Sheet

A table of common commands & variables I use.

CommandExampleDescription
pwdpwd # /path/where/shell/isThe current working directory, i.e. ’this path’ where your shell is.
$CWDCWD=$(pwd) && echo "$CWD"Create a variable $CWD, which is your current working directory (the path where you shell is).
$THIS_DIRTHIS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"A variable with the path where a script that is called exists. Different from $CWD. Where $CWD is the path the shell was called from, $THIS_DIR is the actual path to the script that is being executed where the variable is declared.
mkdir -pmkdir -p ~/path/that/does-not/existCreate a nested directory path where some or all of the parents do not exist yet. Use -pv to show verbose output.
exec $SHELLexec $SHELLReload the current shell. Useful after modifying auto-sourced files like ~/.bashrc. Equivalent to ~/.bashrc and source ~/.bashrc.
while true; do <something>; donewhile true; do echo "Loop!" && sleep 2; doneLoop/repeat a command or phrase until you cancel the command with CTRL-D or CTRL-C, or the loop finishes, exits, or errors.
Last updated on