\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{eugvol} \pdfinfo{ /Title (bash-scripting.pdf) /Creator (Cheatography) /Author (eugvol) /Subject (Bash scripting 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}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \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 scripting Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{eugvol} via \textcolor{DarkBackground}{\uline{cheatography.com/198523/cs/42009/}}} \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}eugvol \\ \uline{cheatography.com/eugvol} \\ \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 16th January, 2024.\\ 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*}{2} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Shebang}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\#!/bin/bash} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\#!/usr/bin/env bash} \tn % Row Count 2 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{5.04 cm} x{2.96 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Variables}} \tn % Row 0 \SetRowColor{LightBackground} `MY\_STR1=Hello` & Hello \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `MY\_STR2=\$MY\_STR1` & Hello \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} `MY\_NUM=45` & 45 \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} `MY\_STR3="Num value is \$MY\_NUM"` & Num value is 45 \tn % Row Count 5 (+ 2) % Row 4 \SetRowColor{LightBackground} `MY\_STR4='Num value is \$MY\_NUM'` & Num value is \$MY\_NUM \tn % Row Count 7 (+ 2) % Row 5 \SetRowColor{white} `MY\_PATH=/etc` & /etc \tn % Row Count 8 (+ 1) % Row 6 \SetRowColor{LightBackground} `MY\_COMMAND=\$(ls \$MY\_PATH)` & result of {\bf{ls /etc}} \tn % Row Count 10 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.008 cm} x{2.736 cm} p{0.792 cm} x{2.664 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{8.4cm}}{\bf\textcolor{white}{Globs}} \tn % Row 0 \SetRowColor{LightBackground} * & Any number of characters inc. none & *.txt & test1.txt \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} ? & Matches single character & ?.txt & a.txt, not ab.txt \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} {[}abc{]} & Matches any one of enclosed characters & {[}ab{]}.txt & a.txt, b.txt, not c.txt \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} {[}a-c{]} & Matches any character in the range & {[}a-c{]}.txt & a.txt, b.txt, c.txt, not d.txt \tn % Row Count 11 (+ 3) % Row 4 \SetRowColor{LightBackground} \seqsplit{?(pattern-list)} & Extended: matches zero or one occurrence of the given patterns & \seqsplit{?(a|b)}.txt & a.txt, b.txt, .txt \tn % Row Count 16 (+ 5) % Row 5 \SetRowColor{white} \seqsplit{*(pattern-list)} & Extended: matches zero or more occurrences of the given patterns & \seqsplit{*(a|b|c)}.txt & a.txt, aa.txt, abac.txt, .txt \tn % Row Count 21 (+ 5) % Row 6 \SetRowColor{LightBackground} \seqsplit{+(pattern-list)} & Extended: matches one or more occurrences of the given patterns & \seqsplit{+(a|b|c)}.txt & a.txt, ab.txt, ba.txt, aaabbbccc.txt, etc \tn % Row Count 26 (+ 5) % Row 7 \SetRowColor{white} \seqsplit{@(pattern-list)} & Extended: matches one of the given patterns & \seqsplit{@(a|b|c)}.txt & a.txt, b.txt, or c.txt \tn % Row Count 29 (+ 3) % Row 8 \SetRowColor{LightBackground} !(pattern-list) & Extended: matches anything except one of the given patterns & !(a|b|c).txt & any .txt files except a.txt, b.txt, c.txt \tn % Row Count 33 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}----} \SetRowColor{LightBackground} \mymulticolumn{4}{x{8.4cm}}{{\bf{shopt -s extglob}} turns on extended globs. \newline {\bf{shopt -s nullglob}} return no output if no matches, otherwise returns the glob pattern. \newline {\bf{shopt -s nocaseglob}} makes globing case-insensitive. \newline {\bf{shopt -s dotglob}} includes filenames starting with a dot (hidden files) in glob patterns.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.8 cm} x{7.2 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{File tests}} \tn % Row 0 \SetRowColor{LightBackground} -d & returns 0 if directory \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} -f & returns 0 if file \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} -e & returns 0 if exists \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} -s & returns 0 if file isn't empty \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} -r & returns 0 if file exists and permissions are granted (-w, -x are also possible) \tn % Row Count 7 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Use either {\bf{test -flag argument}} or {\bf{{[} -flag argument {]}}} format. \newline Use {\bf{echo \$?}} to get the result in bash cmd} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{String tests}} \tn % Row 0 \SetRowColor{LightBackground} `{[} \$a = "Hello" {]}` & returns 0 if strings are equal \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `{[} \$a != \$b {]}` & returns 0 if strings are not equal \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} `{[} -z \$a {]}` & returns 0 if \$a length is zero \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} `{[} -n \$a {]}` & returns 0 if \$a length is non-zero \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} `{[}{[} \$a == \$b {]}{]}` & returns 0 if strings are equal \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} `{[}{[} \$a != \$b {]}{]}` & returns 0 if strings are not equal \tn % Row Count 12 (+ 2) % Row 6 \SetRowColor{LightBackground} `{[}{[} \$a \textgreater{} \$b {]}{]}` & returns 0 if \$a is alphabetically greater than \$b \tn % Row Count 15 (+ 3) % Row 7 \SetRowColor{white} `{[}{[} \$a \textless{} \$b {]}{]}` & returns 0 if \$a is alphabetically less than \$b \tn % Row Count 18 (+ 3) % Row 8 \SetRowColor{LightBackground} `{[}{[} "\$FILENAME" == *.txt {]}{]}` & returns 0 if \$FILENAME matches glob pattern \tn % Row Count 21 (+ 3) % Row 9 \SetRowColor{white} `{[}{[} "Hello !!" =\textasciitilde{} \textasciicircum{}Hello{[}{[}:space:{]}{]}.* {]}{]}` & returns 0 if string matches regex pattern \tn % Row Count 24 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\emph{White spaces}} are important.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.76 cm} x{4.24 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Integers test}} \tn % Row 0 \SetRowColor{LightBackground} \{\{ac\}\}Within {[} {]} & \{\{ac\}\}Within {[}{[} {]}{]} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} -eq & == \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} -ne & != \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} -gt & \textgreater{} \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} -lt & \textless{} \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} -ge & \textgreater{}= \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} -le & \textless{}= \tn % Row Count 7 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Decimals comparison}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{decimal1=3.14 \newline decimal2=2.71 \newline \newline \# Use bc to compare decimal numbers \newline result=\$(echo "\$decimal1 \textgreater{} \$decimal2" | bc -l) \newline \newline if {[} "\$result" -eq 1 {]}; then \newline echo "\$decimal1 is greater than \$decimal2" \newline elif {[} "\$result" -eq 0 {]}; then \newline echo "\$decimal1 is equal to \$decimal2" \newline else \newline echo "\$decimal1 is less than \$decimal2" \newline fi} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.48 cm} x{5.52 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Conditional structures}} \tn % Row 0 \SetRowColor{LightBackground} cmd1 || cmd2 & run cmd1, if fails run cmd2 \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} cmd1 \&\& cmd2 & run cmd1, if ok run cmd2 \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`if {[} \$a -gt 5 {]} \&\& {[} \$b -eq 20 {]}; then\{\{nl\}\} echo "a \textgreater{} 5 AND b=20"\{\{nl\}\}elif {[} \$b -lt 15 {]} || {[} \$c -ge 30 {]}; then\{\{nl\}\} echo "b\textless{}15 OR c\textgreater{}=30"\{\{nl\}\}elif ! {[} \$a -eq 10 {]}; then\{\{nl\}\} echo "a!=10"\{\{nl\}\}else\{\{nl\}\} echo "None of the conditions met"\{\{nl\}\}fi`} \tn % Row Count 8 (+ 6) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`case \$input in\{\{nl\}\} start|START)\{\{nl\}\} echo "Starting the process..."\{\{nl\}\} ;;\{\{nl\}\} stop|STOP)\{\{nl\}\} echo "Stopping the process..."\{\{nl\}\} ;;\{\{nl\}\} *)\{\{nl\}\} echo "Invalid option: \$input"\{\{nl\}\} ;;\{\{nl\}\} esac`} \tn % Row Count 14 (+ 6) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`options=("START" "STOP")\{\{nl\}\}select opt in "\$options{[}@{]}"\{\{nl\}\}do\{\{nl\}\} case \$opt in\{\{nl\}\} START)\{\{nl\}\} echo "Starting the process..."\{\{nl\}\} ;;\{\{nl\}\} STOP)\{\{nl\}\} echo "Stopping the process..."\{\{nl\}\} ;;\{\{nl\}\} *)\{\{nl\}\} echo "Invalid option: \$input"\{\{nl\}\} ;;\{\{nl\}\} esac\{\{nl\}\}done`} \tn % Row Count 21 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.8 cm} x{1.8 cm} x{1.8 cm} x{1.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{8.4cm}}{\bf\textcolor{white}{For loop}} \tn % Row 0 \SetRowColor{LightBackground} Basic & C-style syntax & Over command output & Over array elements \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `for i in 1 2 3 4 5\{\{nl\}\}do\{\{nl\}\} echo "Number \$i"\{\{nl\}\}done` & `for ((i = 1; i \textless{}= 5; i++))\{\{nl\}\}do\{\{nl\}\} echo "Number \$i"\{\{nl\}\}done` & `for user in \$(cat \seqsplit{/etc/passwd} | cut -d ':' -f 1)\{\{nl\}\}do\{\{nl\}\} echo "User: \$user"\{\{nl\}\}done` & \seqsplit{`arr=("apple"} "banana" "cherry")\{\{nl\}\}for fruit in "\$\{arr{[}@{]}\}"\{\{nl\}\}do\{\{nl\}\} echo "Fruit: \$fruit"\{\{nl\}\}done` \tn % Row Count 14 (+ 12) % Row 2 \SetRowColor{LightBackground} Over files & Over range with break & Over range with step & Over string \tn % Row Count 17 (+ 3) % Row 3 \SetRowColor{white} `for file in /path/to/directory/*\{\{nl\}\}do\{\{nl\}\} echo \seqsplit{"Processing} \$file"\{\{nl\}\}done` & `for i in \{1..10\}\{\{nl\}\}do\{\{nl\}\} if {[} "\$i" -eq 5 {]}; then\{\{nl\}\} break\{\{nl\}\} fi\{\{nl\}\} echo "Number \$i"\{\{nl\}\}done` & `for i in \{0..10..2\}\{\{nl\}\}do\{\{nl\}\} echo "Number \$i"\{\{nl\}\}done` & `string="hello"\{\{nl\}\}for char in \$(echo \$string | fold -w1)\{\{nl\}\}do\{\{nl\}\} echo \seqsplit{"Character:} \$char"\{\{nl\}\}done` \tn % Row Count 30 (+ 13) \hhline{>{\arrayrulecolor{DarkBackground}}----} \SetRowColor{LightBackground} \mymulticolumn{4}{x{8.4cm}}{{\bf{break}} and {\bf{continue}} can be used} \tn \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.72 cm} x{2.808 cm} p{0.72 cm} x{2.952 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{8.4cm}}{\bf\textcolor{white}{Special characters and variables}} \tn % Row 0 \SetRowColor{LightBackground} / & Root directory & \$HOME & The current user's home directory \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} . & Current directory & \$PWD & The current working directory \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} \textasciitilde{} & Current user directory & \$PATH & A list of directories separated by colons (:) where the system looks for executable files \tn % Row Count 11 (+ 6) % Row 3 \SetRowColor{white} \textasciitilde{}username & Directory of a user with username & \$USER & The username of the user running the script \tn % Row Count 14 (+ 3) % Row 4 \SetRowColor{LightBackground} .. & Parent directory & \seqsplit{\$HOSTNAME} & The hostname of the machine the script is running on \tn % Row Count 18 (+ 4) % Row 5 \SetRowColor{white} \$0 & The name of the Bash script & \seqsplit{\$RANDOM} & Returns a different random number each time is it referred to \tn % Row Count 22 (+ 4) % Row 6 \SetRowColor{LightBackground} \$1-\$9 & The first 9 arguments to the Bash script & \seqsplit{\$SECONDS} & Number of seconds since the shell was started. Can be used to measure elapsed time of a script \tn % Row Count 28 (+ 6) % Row 7 \SetRowColor{white} \$\# & Number of arguments passed to Bash script & \seqsplit{\$OLDPWD} & Previous directory \tn % Row Count 31 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{p{0.72 cm} x{2.808 cm} p{0.72 cm} x{2.952 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{8.4cm}}{\bf\textcolor{white}{Special characters and variables (cont)}} \tn % Row 8 \SetRowColor{LightBackground} \$@ & All the arguments supplied to the Bash script & \seqsplit{\$LINENO} & Current line number in a script or shell. Used for debugging \tn % Row Count 4 (+ 4) % Row 9 \SetRowColor{white} \$? & The exit status of the most recently run process & \seqsplit{\$SHELL} & Path to the user's default shell \tn % Row Count 8 (+ 4) % Row 10 \SetRowColor{LightBackground} \$\$ & The process ID of the current script & \$UID & User unique ID \tn % Row Count 11 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.52 cm} x{4.48 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Associative arrays}} \tn % Row 0 \SetRowColor{LightBackground} Declare an associative array & `declare -A fruits` \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Adding/appending an item & `fruits{[}apple{]}="red"\{\{nl\}\}fruits{[}banana{]}="yellow"` \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} Reading an item & `echo "The apple is \$\{fruits{[}apple{]}\}"` \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} Changing an item value & `fruits{[}apple{]}="green"` \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} Removing an item & `unset fruits{[}banana{]}` \tn % Row Count 10 (+ 1) % Row 5 \SetRowColor{white} Looping through keys & `for key in "\$\{!fruits{[}@{]}\}"; do\{\{nl\}\} echo "\$key"\{\{nl\}\}done` \tn % Row Count 13 (+ 3) % Row 6 \SetRowColor{LightBackground} Looping through values & `for value in "\$\{fruits{[}@{]}\}"; do\{\{nl\}\} echo "\$value"\{\{nl\}\}done` \tn % Row Count 17 (+ 4) % Row 7 \SetRowColor{white} Looping through keys and values & `for key in "\$\{!fruits{[}@{]}\}"; do\{\{nl\}\} echo "\$key: \$\{fruits{[}\$key{]}\}"\{\{nl\}\}done` \tn % Row Count 21 (+ 4) % Row 8 \SetRowColor{LightBackground} Length of an associative array & `echo \$\{\#fruits{[}@{]}\}` \tn % Row Count 23 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.128 cm} x{3.116 cm} x{2.356 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Arithmetics}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{{\bf{let}} writes the result to a variable but doesn't print it. Used {\emph{only for integers}}.} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `let b=2+5` & `let b=2*5` & `let c=\$a/\$b` \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} `let \seqsplit{c=\$a\%\$b`} & `let c=\$a**\$b` & `let "a = 5 + 2"` \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} `let "a=5+2"` & `let "c = \$a * \$b"` & `let "c = (2 + 3) * 4"` \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} `let a++` & `let b-{}-` & `let c+=1` \tn % Row Count 9 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{3}{x{8.4cm}}{{\bf{expr}} returns the result and prints it. {\emph{Spaces}} are important. Used {\emph{only for integers}}.} \tn % Row Count 11 (+ 2) % Row 6 \SetRowColor{LightBackground} `expr 10 + 5` & `expr 3 * 2` & `expr 10 / 2` \tn % Row Count 13 (+ 2) % Row 7 \SetRowColor{white} `expr \$a - \$b` & `expr 11 \% 5` & `a=\$(expr 11 \% 5)` \tn % Row Count 15 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{{\bf{\$(())}} can also be used for arithmetics {\emph{with integers only}}.} \tn % Row Count 17 (+ 2) % Row 9 \SetRowColor{white} \seqsplit{`a=\$((3+4))`} & `b=\$((\$a-\$b))` & \seqsplit{`c=\$((a*b))`} \tn % Row Count 19 (+ 2) % Row 10 \SetRowColor{LightBackground} \seqsplit{`((b=++a))`} & `((b=-{}-a))` & \seqsplit{`c=\$((b+=3))`} \tn % Row Count 21 (+ 2) % Row 11 \SetRowColor{white} \mymulticolumn{3}{x{8.4cm}}{{\bf{bc}} is used for more complex calculations. It can deal {\emph{with decimals as well}}.} \tn % Row Count 23 (+ 2) % Row 12 \SetRowColor{LightBackground} `a=\$(echo "5 + 3" | bc)` & `b=\$(echo "10.5-2.3" | bc -l)` & `c=\$(echo "sqrt(25)" | bc -l)` \tn % Row Count 26 (+ 3) % Row 13 \SetRowColor{white} `d=\$(echo "2 \textasciicircum{} 3" | bc -l)` & \seqsplit{`rounded\_value=\$(echo} "scale=2; 10/3" | bc)` & \tn % Row Count 29 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{While/Until loops}} \tn % Row 0 \SetRowColor{LightBackground} While & Until \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `counter=1\{\{nl\}\}while {[} \$counter -le 10 {]}\{\{nl\}\}do\{\{nl\}\} echo \$counter\{\{nl\}\} ((counter++))\{\{nl\}\}done` & `counter=1\{\{nl\}\}until {[} \$counter -gt 10 {]}\{\{nl\}\}do\{\{nl\}\} echo \$counter\{\{nl\}\} ((counter++))\{\{nl\}\}done` \tn % Row Count 7 (+ 6) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{While with reading from a file} \tn % Row Count 8 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`while IFS= read -r line; do\{\{nl\}\} echo "\$line"\{\{nl\}\}done \textless{} filename.txt`} \tn % Row Count 10 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Exporting env variables}} \tn % Row 0 \SetRowColor{LightBackground} printenv & Print list of all env variables \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} export VAR="Hello World" & Create env variable (for current session only) \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} echo 'export NEW\_VAR="Hello World"' \textgreater{}\textgreater{} \textasciitilde{}/.bashrc & Create env variable for future sessions \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} source \textasciitilde{}/.bashrc & Reload the shell startup file (required after changing the file with prev. command) \tn % Row Count 13 (+ 5) % Row 4 \SetRowColor{LightBackground} export \seqsplit{PATH=\$PATH:newvalue} & Appending a value (for current session only) \tn % Row Count 16 (+ 3) % Row 5 \SetRowColor{white} unset VAR & Remove env variable \tn % Row Count 17 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.04 cm} x{4.96 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Arrays}} \tn % Row 0 \SetRowColor{LightBackground} Explicit declaration & `declare -a my\_array` \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Implicit creation & `my\_array=(element1 element2 element3)` \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} Read a single element & `echo \$\{my\_array{[}0{]}\}` \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} Read all elements & `echo \$\{my\_array{[}@{]}\}` \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} Changing an element & `my\_array{[}1{]}=new\_element` \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} Appending an element & `my\_array+=(element4)` \tn % Row Count 12 (+ 2) % Row 6 \SetRowColor{LightBackground} Removing an element & `unset my\_array{[}1{]}` \tn % Row Count 14 (+ 2) % Row 7 \SetRowColor{white} Length of an array & `echo \$\{\#my\_array{[}@{]}\}` \tn % Row Count 16 (+ 2) % Row 8 \SetRowColor{LightBackground} Looping through an array & `for element in "\$\{my\_array{[}@{]}\}"; do\{\{nl\}\} echo \$element\{\{nl\}\}done` \tn % Row Count 19 (+ 3) % Row 9 \SetRowColor{white} Sparce array & `declare -a sparse\_array\{\{nl\}\}sparse\_array{[}3{]}="Third Element"\{\{nl\}\}sparse\_array{[}7{]}="Seventh Element"\{\{nl\}\}for index in "\$\{!sparse\_array{[}@{]}\}"; do\{\{nl\}\} echo "Index \$index: \$\{sparse\_array{[}\$index{]}\}"\{\{nl\}\}done` \tn % Row Count 28 (+ 9) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{ECHO and READ}} \tn % Row 0 \SetRowColor{LightBackground} `echo "Hello, World!"` & Hello, World! \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `echo -n "This is a "\{\{nl\}\}echo "single line."` & This is a single line. \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} `echo -e "This is a\textbackslash{}ttab\textbackslash{}tseparated\textbackslash{}ttext."` & This is a tab separated text. \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} `echo -E "This is a\textbackslash{}ttab\textbackslash{}tseparated\textbackslash{}ttext."` & This is a\textbackslash{}ttab\textbackslash{}tseparated\textbackslash{}ttext. \tn % Row Count 11 (+ 3) % Row 4 \SetRowColor{LightBackground} `read var` & reading into a variable \tn % Row Count 13 (+ 2) % Row 5 \SetRowColor{white} `read var1 var2 var3` & reading into several variables \tn % Row Count 15 (+ 2) % Row 6 \SetRowColor{LightBackground} `read -a fruits` & reading into array (whitespace is the default delimiter) \tn % Row Count 18 (+ 3) % Row 7 \SetRowColor{white} `read -p "Enter your name: " name` & reading with prompt \tn % Row Count 20 (+ 2) % Row 8 \SetRowColor{LightBackground} `read -sp "Password: " password` & reading with silent input and prompt \tn % Row Count 22 (+ 2) % Row 9 \SetRowColor{white} `read -n 3 val` & reading with a limited number of characters \tn % Row Count 25 (+ 3) % Row 10 \SetRowColor{LightBackground} `read -t 5 val` & reading with 5s timeout \tn % Row Count 27 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}