{"id":2515,"date":"2021-10-28T19:50:13","date_gmt":"2021-10-28T23:50:13","guid":{"rendered":"https:\/\/dft.wiki\/?p=2515"},"modified":"2026-01-16T09:51:59","modified_gmt":"2026-01-16T14:51:59","slug":"bash-shell-script-cheat-sheet","status":"publish","type":"post","link":"https:\/\/dft.wiki\/?p=2515","title":{"rendered":"Bash Script Cheat Sheet"},"content":{"rendered":"<p>This is a quick reference guide for creating automation scripts and clever averaging of bash features for issuing efficient and reliable commands.<\/p>\n<p>Some might have an example, while the most straightforward one won&#8217;t.<\/p>\n<hr \/>\n<p><strong>BASIC USAGE OF VARIABLES, STRINGS, AND NUMBERS<\/strong><\/p>\n<pre>echo [string]<\/pre>\n<pre>[variable]=[string]<\/pre>\n<pre>echo $[variable]<\/pre>\n<pre>$(( [expression] ))<\/pre>\n<p><strong>E.g.<\/strong> <code>echo $((4*5))<\/code><\/p>\n<p>Operators available:<\/p>\n<ul>\n<li>+\n<ul>\n<li>Addition<\/li>\n<\/ul>\n<\/li>\n<li>&#8211;\n<ul>\n<li>Subtraction<\/li>\n<\/ul>\n<\/li>\n<li>*\n<ul>\n<li>Multiplicaton<\/li>\n<\/ul>\n<\/li>\n<li>\/\n<ul>\n<li>Division<\/li>\n<\/ul>\n<\/li>\n<li>%\n<ul>\n<li>Remainder of a division<\/li>\n<\/ul>\n<\/li>\n<li>**\n<ul>\n<li>Exponentiation<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre>$( bc -l &lt;&lt;&lt; [expression] )<\/pre>\n<p>E.g. <code>$( bc -l &lt;&lt;&lt; 10\/3 )<\/code><\/p>\n<pre>let [variable]=[expression]<\/pre>\n<pre>declare -i [variable]<\/pre>\n<ul>\n<li>MAKING A VARIABLE ACCESSIBLE TO A CHILD PROCESS (LIKE ENVIRONMENT VARIABLES)<\/li>\n<\/ul>\n<pre>export [variable name]=[value]<\/pre>\n<ul>\n<li>EXECUTING A COMMAND IN THE BACKGROUND<\/li>\n<\/ul>\n<pre>[command] &amp;<\/pre>\n<ul>\n<li>LISTING BACKGROUND PROCESSED<\/li>\n<\/ul>\n<pre>jobs<\/pre>\n<ul>\n<li>BRINGING THE BACKGROUND PROCESS TO THE FOREGROUND<\/li>\n<\/ul>\n<pre>fg [job number]<\/pre>\n<ul>\n<li>RUN A SECOND COMMAND IF THE FIRST IS SUCCESSFUL<\/li>\n<\/ul>\n<pre>[command1] &amp;&amp; [command2]<\/pre>\n<ul>\n<li>RUN A SECOND COMMAND IF THE FIRST FAILS<\/li>\n<\/ul>\n<pre>[command1] || [command2]<\/pre>\n<hr \/>\n<p><strong>CONDITIONAL STATEMENTS IN A SINGLE LINE<\/strong><\/p>\n<ul>\n<li>IF<\/li>\n<\/ul>\n<pre><strong>if<\/strong> [condition]; <strong>then<\/strong> [command]; <strong>fi<\/strong><\/pre>\n<pre><strong>if<\/strong> [condition]; <strong>then<\/strong> [command1]; <strong>else<\/strong> [command2]; <strong>fi<\/strong><\/pre>\n<pre><strong>if<\/strong> [condition1]; <strong>then<\/strong> [command1]; <strong>elif<\/strong> [condition2]; <strong>then<\/strong> [command2]; <strong>else<\/strong> [command3]; <strong>fi<\/strong><\/pre>\n<ul>\n<li>WHILE LOOP<\/li>\n<\/ul>\n<pre><strong>while<\/strong> [condition]; <strong>do<\/strong> [command]; <strong>done<\/strong><\/pre>\n<ul>\n<li>UNTIL LOOP (opposite of WHILE)<\/li>\n<\/ul>\n<pre><strong>until<\/strong> [condition]; <strong>do<\/strong> [command]; <strong>done<\/strong><\/pre>\n<ul>\n<li>FOR LOOP<\/li>\n<\/ul>\n<pre><strong>for<\/strong> [variable] <strong>in<\/strong> [list]; <strong>do<\/strong> [command]; <strong>done<\/strong><\/pre>\n<p>E.g. <code>for name in John Mike Sarah; do echo \"Hello, $name\"; done<\/code><\/p>\n<p>E.g. <code>for X in {1..10}; do echo $X' &gt;&gt; output.txt; done<\/code><\/p>\n<p>E.g. <code>for X in $(cat list.txt); do echo $ID &gt;&gt; output.txt; done<\/code><\/p>\n<pre><strong>for<\/strong> <strong>((<\/strong> [initialisation]; [condition]; [increment] <strong>))<\/strong>; <strong>do<\/strong> [command]; <strong>done<\/strong><\/pre>\n<p>E.g. <code>for ((i=0; i&lt;5; i++)); do echo $i; done<\/code><\/p>\n<p><strong>Note:<\/strong> for any of the looping above the consider using the following commands as applicable:<\/p>\n<ol>\n<li>&#8220;<strong>break<\/strong>&#8221; to exit a loop prematurely.<\/li>\n<li>&#8220;<strong>continue<\/strong>&#8221; to skip the current iteration of a loop and move to the next one.<\/li>\n<\/ol>\n<p>While the looping above will run one at a time, the example below can parallelize the execution of the commands, keeping a defined number of simultaneous executions.<\/p>\n<pre>cat [source_file] | xargs -P <strong>[parallel]<\/strong> -I {} sh -c '{} &gt;&gt; [output_file]'<\/pre>\n<p>E.g. <code>cat commands.list | xargs -P <strong>10<\/strong> -I {} sh -c '{} &gt;&gt; output.txt'<\/code><\/p>\n<ul>\n<li>CASE<\/li>\n<\/ul>\n<pre><strong>case<\/strong> [variable] <strong>in<\/strong> [pattern1]<strong>)<\/strong> [command1];; [pattern2]<strong>)<\/strong> [command2];; <strong>esac<\/strong><\/pre>\n<p>E.g.\u00a0<code>case $option in \"a\"|\"A\") echo \"a or A\";; \"b\"|\"B\") echo \"b or B\";; *) echo \"Not a valid option.\"; esac<br \/>\n<\/code><\/p>\n<ul>\n<li>SELECT<\/li>\n<\/ul>\n<pre><strong>select<\/strong> [variable] <strong>in<\/strong> [list]; <strong>do<\/strong> [command]; <strong>done<\/strong><\/pre>\n<p>E.g. <code>select X in pwd uptime whoami; do $X; break; done<\/code><\/p>\n<hr \/>\n<p><strong>PROCESSING ARGUMENTS<\/strong><\/p>\n<ul>\n<li>GETOPTS &#8211; a built-in Bash utility used to parse short options (like -a, -b, -c) and their optional arguments from command-line arguments.<\/li>\n<\/ul>\n<pre><strong>getopts<\/strong> [options] [variable]<\/pre>\n<p>E.g.<\/p>\n<pre>#!\/bin\/bash\r\nwhile getopts \"a:b:c\" opt; do\r\n  case $opt in\r\n    a) echo \"Option -a, argument: $OPTARG\" ;;\r\n    b) echo \"Option -b, argument: $OPTARG\" ;;\r\n    c) echo \"Option -c, no argument\" ;;\r\n    ?) echo \"Usage: $0 [-a arg] [-b arg] [-c]\" ;;\r\n  esac\r\ndone<\/pre>\n<ul>\n<li>SHIFT &#8211; this command is used to shift the positional parameters to the left, it removes N arguments and shifts all the remaining arguments down by one.<\/li>\n<\/ul>\n<pre>shift N<\/pre>\n<p>E.g.<\/p>\n<pre>#!\/bin\/bash\r\nwhile [ \"$#\" -gt 0 ]; do\r\n  echo \"Argument: $1\"\r\n  shift # Shift the arguments by one\r\ndone<\/pre>\n<hr \/>\n<p><strong>SIGNALS AND STATUS<\/strong><\/p>\n<ul>\n<li>TRAP<\/li>\n<\/ul>\n<pre>trap [command] [signal]<\/pre>\n<p>E.g.<\/p>\n<ul>\n<li>EXIT<\/li>\n<\/ul>\n<pre>exit [status]<\/pre>\n<p>E.g.<\/p>\n<ul>\n<li>RETURN<\/li>\n<\/ul>\n<pre>return [status]<\/pre>\n<p>E.g.<\/p>\n<hr \/>\n<p><strong>SETTING PARAMETERS FOR THE SCRIPT EXECUTION ENVIRONMENT<\/strong><\/p>\n<ul>\n<li>SET\/UNSET to exit the script if any command returns a non-zero exit status.<\/li>\n<\/ul>\n<pre>set -e<\/pre>\n<pre>set +e<\/pre>\n<ul>\n<li>SET\/UNSET to exit the script if any variable is accessed without being set.<\/li>\n<\/ul>\n<pre>set -u<\/pre>\n<pre>set +u<\/pre>\n<ul>\n<li>SET\/UNSET to print each command before it is executed<\/li>\n<\/ul>\n<pre>set -x<\/pre>\n<pre>set +x<\/pre>\n<hr \/>\n<p><strong>REDIRECTING OUTPUTS<\/strong><\/p>\n<ul>\n<li>STANDARD OUTPUT (STDOUT)<\/li>\n<\/ul>\n<pre>[command] &gt; [somewhere]<\/pre>\n<ul>\n<li>STANDARD ERROR (STDERR)<\/li>\n<\/ul>\n<pre>[command] 2&gt; [somewhere]<\/pre>\n<ul>\n<li>OUTPUT STDERR INTO STDOUT<\/li>\n<\/ul>\n<pre>[command] &gt; [somewhere] 2&gt;&amp;1<\/pre>\n<ul>\n<li>DUMPING BOTH OUTPUTS<\/li>\n<\/ul>\n<pre>[command] &amp;&gt; \/dev\/null<\/pre>\n<ul>\n<li>PIPING<\/li>\n<\/ul>\n<pre>[command] 2&gt;&amp;1 | tee [file]<\/pre>\n<ul>\n<li>WRITING TO A FILE<\/li>\n<\/ul>\n<pre>[command] &gt; [file]<\/pre>\n<hr \/>\n<p><strong>SCRIPTING AND AUTOMATION<\/strong><\/p>\n<ul>\n<li>DEFINE INTERPRETER TO BASH<\/li>\n<\/ul>\n<pre>#!\/bin\/bash<\/pre>\n<ul>\n<li>SET THE SCRIPT AS EXECUTABLE<\/li>\n<\/ul>\n<pre>chmod +x [script name]<\/pre>\n<ul>\n<li>EXECUTING A SCRIPT<\/li>\n<\/ul>\n<pre>.\/[script name]<\/pre>\n<ul>\n<li>READ USER&#8217;S INPUT<\/li>\n<\/ul>\n<pre>read [options] [variable]<\/pre>\n<p>Available options (they are not required):<\/p>\n<ul>\n<li>-p\n<ul>\n<li>Prompts a message.<\/li>\n<\/ul>\n<\/li>\n<li>-s\n<ul>\n<li>Hides the entered characters as if they were secret.<\/li>\n<\/ul>\n<\/li>\n<li>-t\n<ul>\n<li>Times out after a number of seconds.<\/li>\n<\/ul>\n<\/li>\n<li>-d\n<ul>\n<li>Define the delimiter instead of the default new line.<\/li>\n<\/ul>\n<\/li>\n<li>-r\n<ul>\n<li>Allow input special characters such as backslash.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>E.g. <code>read -p \"Enter your name: \" name<\/code><\/p>\n<p>E.g. <code>read -s -p \"Enter your password: \" password<\/code><\/p>\n<p>E.g. <code>read -t 10 -p \"Enter your input within 10 seconds: \" input<\/code><\/p>\n<p>E.g. <code>read -d '|' -p \"Enter your name and age separated by '|': \" name age<\/code><\/p>\n<hr \/>\n<p><strong>COPY PASTE TIPS<\/strong><\/p>\n<ul>\n<li>QUICK FORMATTING PROMPT<\/li>\n<\/ul>\n<pre>BLACK=`tput setaf 0`\r\nRED=`tput setaf 1`\r\nGREEN=`tput setaf 2`\r\nYELLOW=`tput setaf 3`\r\nBLUE=`tput setaf 4`\r\nMAGENTA=`tput setaf 5`\r\nCYAN=`tput setaf 6`\r\nWHITE=`tput setaf 7`\r\n\r\nBOLD=`tput bold`\r\nRESET=`tput sgr0`\r\n\r\necho -e \"hello ${RED}some red text${RESET} world\"<\/pre>\n<ul>\n<li>DIALOGUE BOXES<\/li>\n<\/ul>\n<pre>#!\/bin\/bash\r\nfunction box_out() {\r\n  input_char=$(echo \"$@\" | wc -c)\r\n  line=$(for i in `seq 0 $input_char`; do printf \"\u2500\"; done)\r\n  tput bold\r\n  space=${line\/\/\u2500\/ }\r\n  echo \" \u250c${line}\u2510\"\r\n  printf ' \u2502 ' ; echo -n \"$@\"; printf \"%s\\n\" ' \u2502';\r\n  echo \" \u2514${line}\u2518\"\r\n  tput sgr 0\r\n}\r\n\r\nfunction box_success() {\r\n  tput bold\r\n  tput setaf 2\r\n  echo \" \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\"\r\n  echo \" \u2502 Success! \u2502\"\r\n  echo \" \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\"\r\n  tput sgr0\r\n}\r\n\r\nfunction box_failed() {\r\n  tput bold\r\n  tput setaf 1\r\n  echo \" \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\"\r\n  echo \" \u2502 Failed! \u2502\"\r\n  echo \" \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\"\r\n  tput sgr0\r\n}\r\n\r\nbox_out \"Hello there!\"\r\nbox_success\r\nbox_failed<\/pre>\n<ul>\n<li>QUICK SINGLE-LINE FORMATTED PRINTING<\/li>\n<\/ul>\n<pre>red() { echo -e \"\\033[31m [ $1 ] \\033[0m\" ; }\r\ngreen() { echo -e \"\\033[32m [ $1 ] \\033[0m\" ; }\r\nblue() { echo -e \"\\033[34m [ $1 ] \\033[0m\" ; }\r\nyellow() { echo -e \"\\033[33m [ $1 ] \\033[0m\" ; }\r\npurple() { echo -e \"\\033[35m [ $1 ] \\033[0m\" ; }\r\ncyan() { echo -e \"\\033[36m [ $1 ] \\033[0m\" ; }\r\n\r\nred_bold() { echo -e \"\\033[1;31m [ $1 ] \\033[0m\" ; }\r\ngreen_bold() { echo -e \"\\033[1;32m [ $1 ] \\033[0m\" ; }\r\nblue_bold() { echo -e \"\\033[1;34m [ $1 ] \\033[0m\" ; }\r\nyellow_bold() { echo -e \"\\033[1;33m [ $1 ] \\033[0m\" ; }\r\npurple_bold() { echo -e \"\\033[1;35m [ $1 ] \\033[0m\" ; }\r\ncyan_bold() { echo -e \"\\033[1;36m [ $1 ] \\033[0m\" ; }\r\n\r\nred_italic() { echo -e \"\\033[3;31m [ $1 ] \\033[0m\" ; }\r\ngreen_italic() { echo -e \"\\033[3;32m [ $1 ] \\033[0m\" ; }\r\nblue_italic() { echo -e \"\\033[3;34m [ $1 ] \\033[0m\" ; }\r\nyellow_italic() { echo -e \"\\033[3;33m [ $1 ] \\033[0m\" ; }\r\npurple_italic() { echo -e \"\\033[3;35m [ $1 ] \\033[0m\" ; }\r\ncyan_italic() { echo -e \"\\033[3;36m [ $1 ] \\033[0m\" ; }\r\n\r\nred_underline() { echo -e \" \\033[4;31m[ $1 ]\\033[0m \" ; }\r\ngreen_underline() { echo -e \" \\033[4;32m[ $1 ]\\033[0m \" ; }\r\nblue_underline() { echo -e \" \\033[4;34m[ $1 ]\\033[0m \" ; }\r\nyellow_underline() { echo -e \" \\033[4;33m[ $1 ]\\033[0m \" ; }\r\npurple_underline() { echo -e \" \\033[4;35m[ $1 ]\\033[0m \" ; }\r\ncyan_underline() { echo -e \" \\033[4;36m[ $1 ]\\033[0m \" ; }<\/pre>\n<pre>red \"This is an error message.\"\r\ngreen \"This is a success message.\"\r\nblue_bold \"This is an informational message.\"\r\nyellow_bold \"This is a warning message.\"\r\npurple_italic \"This is a custom message.\"\r\ncyan_underline \"This is also a custom message.\"<\/pre>\n<ul>\n<li>BRACES EXPANSION<\/li>\n<\/ul>\n<pre>echo abc{mnp}xyz\r\nmkdir num_{0..z}<\/pre>\n<ul>\n<li>CHECK EXIT STATUS OF PREVIOUS COMMAND<\/li>\n<\/ul>\n<pre>echo $?<\/pre>\n<p>OR<\/p>\n<pre>case $? in\r\n  0) echo \"OK\";;\r\n  1) echo \"Err\";;\r\n  *) echo \"Unhandled error\";;\r\nesac<\/pre>\n<ul>\n<li>RUNNING SCRIPT IN CURRENT CONTEXT OR LOADING VARIABLES PLUS FUNCTIONS<\/li>\n<\/ul>\n<pre>source .\/scriptName.sh argument1 argument2<\/pre>\n<p>OR<\/p>\n<pre>. .\/scriptName.sh argument1 argument2<\/pre>\n<ul>\n<li>RESTORING SESSION SETTINGS<\/li>\n<\/ul>\n<pre>. .profile<\/pre>\n<ul>\n<li>HIDE BLINKING CURSOR DURING SCRIPT EXECUTION AND RESTORE AFTERWARDS<\/li>\n<\/ul>\n<pre>tput civis<\/pre>\n<pre>tput cnorm<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is a quick reference guide for creating automation scripts and clever averaging of bash [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,6],"tags":[],"class_list":["post-2515","post","type-post","status-publish","format-standard","hentry","category-linux","category-raspberry-pi"],"_links":{"self":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2515","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2515"}],"version-history":[{"count":17,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2515\/revisions"}],"predecessor-version":[{"id":5232,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2515\/revisions\/5232"}],"wp:attachment":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}