\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{blakecromar} \pdfinfo{ /Title (bash.pdf) /Creator (Cheatography) /Author (blakecromar) /Subject (Bash Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{6F6FA3} \definecolor{LightBackground}{HTML}{F6F6F9} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Bash Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{blakecromar} via \textcolor{DarkBackground}{\uline{cheatography.com/218070/cs/48235/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}blakecromar \\ \uline{cheatography.com/blakecromar} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 26th June, 2026.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{char*}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{char* my\_string = "hello"; \newline my\_string + 1; // points to "ello" \newline my\_string + 2; // points to "llo"} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{A pointer to a character, typically used to point to the start of a string. \newline \newline Strings in C are just arrays of characters ending with a null terminator \textbackslash{}0. \newline \newline Adding a number to the pointer moves it forward by that many characters.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{sleep}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{sleep 1 \# pause for 1 second \newline sleep 30 \# pause for 30 seconds \newline sleep 0.5 \# pause for half a second} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Pauses the script for a specified number of seconds. \newline \newline Used to simulate delays, timeouts, or hung processes in testing.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{/dev/null}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{echo "discard this" \textgreater{} /dev/null \# output is discarded \newline command 2\textgreater{} /dev/null \# discard error output} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{A special file on Linux that discards anything written to it. \newline \newline Commonly used to suppress unwanted output.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Array Declaration ()}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{my\_array=("first" "second" "third") \newline echo \$\{my\_array{[}0{]}\} \# prints "first" \newline echo \$\{my\_array{[}1{]}\} \# prints "second" \newline echo \$\{my\_array{[}2{]}\} \# prints "third"} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Parentheses are used to declare an array in bash. \newline \newline Each space-separated value becomes its own element, accessible by index starting at 0. \newline \newline "\$@" can be used to populate an array with all script arguments.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{case ... in ... esac}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{case "\$\{MY\_VARIABLE\}" in \newline \textless{}pattern1\textgreater{}) do\_something ;; \newline \textless{}pattern2\textgreater{}) do\_something\_else ;; \newline \textless{}*\textgreater{}) default\_action ;; \newline esac} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Bash's switch statement, matching a value against a set of patterns. \newline esac closes the case block (it's "case" spelled backwards). \newline Each pattern ends with ) and each case ends with ;;. \newline \newline Note: *) is the catch-all pattern in a case statement, matching anything not already matched. Always placed last so specific patterns are checked first.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{\$@}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#!/usr/bin/env bash \newline echo "\$@" \# prints all arguments \newline some\_command "\$@" \# passes all arguments to another command} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Represents all arguments passed to a script or function. \newline \newline Quoting it with "\$@" preserves arguments that contain spaces as single units. \newline \newline Commonly used to pass all arguments along to another command or store them.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{\# Length Operator}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{my\_string="hello" \newline \$\{\#my\_string\} \# 5 (number of characters) \newline \newline my\_array=("a" "b" "c") \newline \$\{\#my\_array{[}@{]}\} \# 3 (number of elements)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{When used inside \$\{\}, the \# returns the length of a variable or array. \newline \newline For strings it returns the number of characters, for arrays the number of elements.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{;}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{command1 ; command2 \# command2 runs regardless \newline command1 \&\& command2 \# command2 only runs if command1 succeeded \newline rm -f "\$src" ; exit 0 \# delete file then exit} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Separates two commands on the same line, running them sequentially. \newline \newline The second command always runs regardless of whether the first succeeded or failed. \newline \newline Different from \&\& which only runs the second command if the first succeeded.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{exit}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{exit 0 \# success \newline exit 1 \# generic failure \newline exit 23 \# custom failure code} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Exits the script with a return code. \newline \newline 0 means success, any non-zero value means failure. \newline \newline The exit code can be checked by the calling process to determine if the script succeeded.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{""}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{"\$\{MY\_VARIABLE\}" \# preserved as single unit \newline some\_command "\$@" \# preserves arguments with spaces} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Quotes preserve a value as a single unit if it contains spaces. \newline \newline Without quotes, spaces would split the value into separate arguments.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{\$\{\}}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\$\{MY\_VARIABLE\} \newline echo "Hello \$\{NAME\}world" \# without braces NAME and world would merge} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Curly braces denote a variable expansion in bash. \newline \newline Used to explicitly mark the boundaries of a variable name. \newline \newline Required when combining a variable with other characters.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Array Indexing}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{my\_array=("first" "second" "third") \newline \# {[}0{]} {[}1{]} {[}2{]} \newline \newline \$\{my\_array{[}0{]}\} \# "first" \newline \$\{my\_array{[}2{]}\} \# "third" (last element) \newline \$\{my\_array{[}\$\{\#my\_array{[}@{]}\} - 1{]}\} \# "third" (last element dynamically) \newline \$\{my\_array{[}\$\{\#my\_array{[}@{]}\} - 2{]}\} \# "second" (second to last dynamically)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Arrays in bash start at index 0, not 1. \newline \newline The last element is always at index length - 1. \newline \newline The second to last element is at index length - 2.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{\$(())}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\$(( 5 + 2 )) \# 7 \newline \$(( 10 - 3 )) \# 7 \newline \$(( 4 * 2 )) \# 8} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Performs arithmetic in bash. \newline \newline The result of the calculation is used as a value.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{\textgreater{}\textgreater{}}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{echo "new line" \textgreater{}\textgreater{} existing\_file.txt \# appends to file \newline echo "new line" \textgreater{} existing\_file.txt \# overwrites file} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Appends output to a file without overwriting existing content. \newline \newline Use \textgreater{} instead to overwrite the file completely.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{\textgreater{}\&2}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{echo "something went wrong" \textgreater{}\&2 \# sends to stderr \newline echo "normal output" \# sends to stdout} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Redirects output to stderr (standard error) instead of stdout (standard output). \newline \newline Used for error messages so they can be separated from normal output.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{:-}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\$\{MY\_VARIABLE:-default\_value\} \# uses default\_value if MY\_VARIABLE is unset \newline \$\{MY\_VARIABLE:-/some/path\} \# uses /some/path if MY\_VARIABLE is unset} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Provides a default value if the variable is unset or empty. \newline \newline The value after :- is used as a fallback.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{rm}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{rm file.txt \# removes file, errors if not found \newline rm -f file.txt \# removes file, no error if not found \newline rm -rf directory/ \# removes directory and all contents forcefully} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Removes a file from the filesystem. \newline \newline Without flags it will prompt for confirmation on protected files and error on missing files. \newline \newline A `-f` flag forces deletion — suppresses all prompts and ignores missing files, making it safe to use in scripts where the file may or may not exist.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}