<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Shell Scripting | Haobin Tan</title><link>https://haobin-tan.netlify.app/tags/shell-scripting/</link><atom:link href="https://haobin-tan.netlify.app/tags/shell-scripting/index.xml" rel="self" type="application/rss+xml"/><description>Shell Scripting</description><generator>Hugo Blox Builder (https://hugoblox.com)</generator><language>en-us</language><lastBuildDate>Thu, 15 Aug 2024 00:00:00 +0000</lastBuildDate><image><url>https://haobin-tan.netlify.app/media/icon_hu7d15bc7db65c8eaf7a4f66f5447d0b42_15095_512x512_fill_lanczos_center_3.png</url><title>Shell Scripting</title><link>https://haobin-tan.netlify.app/tags/shell-scripting/</link></image><item><title>Shell Scripting</title><link>https://haobin-tan.netlify.app/docs/coding/linux/getting-started/03_intro_shell_scripting/</link><pubDate>Thu, 15 Aug 2024 00:00:00 +0000</pubDate><guid>https://haobin-tan.netlify.app/docs/coding/linux/getting-started/03_intro_shell_scripting/</guid><description>&lt;h2 id="shell-scripting-basics">Shell Scripting Basics&lt;/h2>
&lt;p>A &lt;strong>script&lt;/strong> is a list of commands that can be interpreted and run by a program called &lt;strong>scripting  language&lt;/strong>, which is usually not compiled but interpreted at runtime instead.&lt;/p>
&lt;ul>
&lt;li>Generally slower to run than compiled languages, but they are also much easier and faster to develop&lt;/li>
&lt;li>Widely used to automate processes, such as
&lt;ul>
&lt;li>ETL jobs&lt;/li>
&lt;li>File backups and archiving&lt;/li>
&lt;li>System administration tasks&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;p>Shell script is an executable text file with an interpreter directive, a.k.a. &amp;ldquo;shebang&amp;rdquo; (&lt;code>#!&lt;/code>) directive, which has the form &amp;ldquo;pound, bang, interpreter&amp;rdquo; plus an optional argument&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="cp">#!interpreter [optional-arg]
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ul>
&lt;li>&lt;code>interpreter&lt;/code>: An absolute path to an executable program&lt;/li>
&lt;li>&lt;code>optional-arg&lt;/code>: A string representing a single argument&lt;/li>
&lt;/ul>
&lt;p>Example:&lt;/p>
&lt;ul>
&lt;li>&lt;code>#!/bin/sh&lt;/code> invokes the Bourne shell or other compatible shell program, from the bin directory.&lt;/li>
&lt;li>&lt;code>#!/bin/bash&lt;/code> invokes the BAsh shell&lt;/li>
&lt;li>&lt;code>#!/usr/bin/env python3&lt;/code>&lt;/li>
&lt;/ul>
&lt;h2 id="shell-variables">Shell Variables&lt;/h2>
&lt;p>&lt;strong>Shell variables&lt;/strong> offer a powerful way to store and later access or modify information such as numbers, character strings, and other data structures by name.&lt;/p>
&lt;p>Example&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ &lt;span class="nv">firstname&lt;/span>&lt;span class="o">=&lt;/span>Jeff
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">$ &lt;span class="nb">echo&lt;/span> &lt;span class="nv">$firstname&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">Jeff
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The first line assigns the value &lt;code>Jeff&lt;/code> to a new variable called &lt;code>firstname&lt;/code>. The next line accesses and displays the value of the variable, using the &lt;code>echo&lt;/code> command along with the special character &lt;code>$&lt;/code> in front of the variable name to extract its value, which is the string &lt;code>Jeff&lt;/code>. Thus, we have created a new shell variable called &lt;code>firstname&lt;/code> for which the value is &lt;code>Jeff&lt;/code>.&lt;/p>
&lt;p>Another way to create a shell variable is to use the &lt;code>read&lt;/code> command, which reads user input into a shell variable.&lt;/p>
&lt;p>Example:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ &lt;span class="nb">read&lt;/span> lastname
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">Grossman
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">$ &lt;span class="nb">echo&lt;/span> &lt;span class="nv">$lastname&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">Grossman
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The &lt;code>read&lt;/code> command is particularly useful in shell scripting. You can use it within a shell script to prompt users to input information, which is then stored in a shell variable and available for use by the shell script while it is running.&lt;/p>
&lt;h2 id="filters-pipes-and-variables">Filters, Pipes, and Variables&lt;/h2>
&lt;h4 id="filters">Filters&lt;/h4>
&lt;p>&lt;strong>Filters&lt;/strong> are shell commands, which&lt;/p>
&lt;ul>
&lt;li>Take input from standard input&lt;/li>
&lt;li>Send output to standard output&lt;/li>
&lt;li>Transform input data into output data&lt;/li>
&lt;li>Examples: &lt;code>wc&lt;/code>. &lt;code>cat&lt;/code>. &lt;code>more&lt;/code>, &lt;code>head&lt;/code>, &lt;code>sort&lt;/code>, &lt;code>grep&lt;/code>&lt;/li>
&lt;li>Filters can be chain together!&lt;/li>
&lt;/ul>
&lt;h4 id="pipe-pipeline">Pipe (Pipeline)&lt;/h4>
&lt;p>&lt;strong>Pipe command (&lt;code>|&lt;/code>)&lt;/strong> : for chaining filter commands&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Use pattern&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="o">[&lt;/span>&lt;span class="nb">command&lt;/span> 1&lt;span class="o">]&lt;/span> &lt;span class="p">|&lt;/span> &lt;span class="o">[&lt;/span>&lt;span class="nb">command&lt;/span> 2&lt;span class="o">]&lt;/span> &lt;span class="p">|&lt;/span> &lt;span class="o">[&lt;/span>&lt;span class="nb">command&lt;/span> 3&lt;span class="o">]&lt;/span> ... &lt;span class="p">|&lt;/span> &lt;span class="o">[&lt;/span>&lt;span class="nb">command&lt;/span> n&lt;span class="o">]&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>E.g.,&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">command&lt;/span> &lt;span class="m">1&lt;/span> &lt;span class="p">|&lt;/span> &lt;span class="nb">command&lt;/span> &lt;span class="m">2&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Output of &lt;code>command 1&lt;/code> is input of &lt;code>command 2&lt;/code>&lt;/p>
&lt;p>&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/%E6%88%AA%E5%B1%8F2024-08-18%2014.46.10.png" alt="截屏2024-08-18 14.46.10">&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Example&lt;/p>
&lt;p>&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/%E6%88%AA%E5%B1%8F2024-08-18%2014.01.00.png" alt="截屏2024-08-18 14.01.00">&lt;/p>
&lt;/li>
&lt;/ul>
&lt;h4 id="variables">Variables&lt;/h4>
&lt;ul>
&lt;li>
&lt;p>Scope limited to shell&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>set&lt;/code>: List all variables and their definitions that are visible to the current shell&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Define shell variables&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nv">var_name&lt;/span>&lt;span class="o">=&lt;/span>value &lt;span class="c1"># Note: NO space around &amp;#34;=&amp;#34;!&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Clear/Delete a variable\&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">unset&lt;/span> var_name
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Example&lt;/p>
&lt;p>&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/%E6%88%AA%E5%B1%8F2024-08-18%2014.09.56.png" alt="截屏2024-08-18 14.09.56">&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Environment variables&lt;/strong>: Just like shell variables, except they have extended scope. They persist in any child processes spawned by the shell in which they originate.&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Extend shell variable to environment variable:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">export&lt;/span> var_name
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Example&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">export&lt;/span> GREETINGS
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>makes the &lt;code>GREETINGS&lt;/code> variable defined in the example above an environment variable&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>env&lt;/code>: List all environment variables&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h2 id="useful-features-of-the-bash-shell">Useful Features of the Bash Shell&lt;/h2>
&lt;h3 id="metacharacters">Metacharacters&lt;/h3>
&lt;p>&lt;strong>Metacharacters&lt;/strong> are special characters that have meaning to the shell.&lt;/p>
&lt;ul>
&lt;li>
&lt;p>&lt;code>#&lt;/code>: Precedes a comment that the shell ignores&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>;&lt;/code>: Seprate commands typed on the same line, e.g.,&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ &lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;Hello&amp;#34;&lt;/span> &lt;span class="p">;&lt;/span> whoami
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;/li>
&lt;li>
&lt;p>&lt;code>*&lt;/code>: Filename expansion wildcard, represents any number of consecutive characters within a filename pattern&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>?&lt;/code>: Single character wildcard in filename expansion (a single-character version of &lt;code>*&lt;/code> ), e.g.,&lt;/p>
&lt;p>&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/%E6%88%AA%E5%B1%8F2024-08-18%2014.59.31.png" alt="截屏2024-08-18 14.59.31">&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>\&lt;/code>: Escape unique character interpretation&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>&amp;quot; &amp;quot;&lt;/code>: Interpret literally, interpret metacharacters within string&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>' '&lt;/code>: Interpret literally, escape all metacharacters within string&lt;/p>
&lt;/li>
&lt;/ul>
&lt;p>Example:&lt;/p>
&lt;p>&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/%E6%88%AA%E5%B1%8F2024-08-18%2015.36.03.png" alt="截屏2024-08-18 15.36.03">&lt;/p>
&lt;h3 id="io-redirection">I/O redirection&lt;/h3>
&lt;p>&lt;strong>Input/Output&lt;/strong>, or &lt;strong>I/O redirection&lt;/strong>, refers to a set of features used for redirecting either the standard input, the keyboard, or the standard output, the terminal.&lt;/p>
&lt;ul>
&lt;li>&lt;code>&amp;gt;&lt;/code>: Redirect output to the file. It also creates the file if it doesn’t exist and overwrites its contents if it already exists.&lt;/li>
&lt;li>&lt;code>&amp;gt;&amp;gt;&lt;/code>: Append output to the file&lt;/li>
&lt;li>&lt;code>2&amp;gt;&lt;/code>: Ridirect standard error to file&lt;/li>
&lt;li>&lt;code>2&amp;gt;&amp;gt;&lt;/code>: Append standard error to file&lt;/li>
&lt;li>&lt;code>&amp;lt;&lt;/code>: Redirect file contents to standard input&lt;/li>
&lt;/ul>
&lt;p>Example&lt;/p>
&lt;p>&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/%E6%88%AA%E5%B1%8F2024-08-18%2015.40.10.png" alt="截屏2024-08-18 15.40.10">&lt;/p>
&lt;h3 id="command-substitution">Command substitution&lt;/h3>
&lt;ul>
&lt;li>
&lt;p>Replace the command with its output: &lt;code>$(command)$&lt;/code> or &lt;code>command&lt;/code>. Example:&lt;/p>
&lt;p>&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/%E6%88%AA%E5%B1%8F2024-08-18%2015.42.02.png" alt="截屏2024-08-18 15.42.02">&lt;/p>
&lt;/li>
&lt;/ul>
&lt;h3 id="command-line-arguments">Command line arguments&lt;/h3>
&lt;ul>
&lt;li>Program arguments specified on the command line&lt;/li>
&lt;li>A way to pass arguments to a shell script&lt;/li>
&lt;/ul>
&lt;p>Example&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">$ ./MyBashScript.sh arg1 arg2
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="batch-vs-concurrent-modes">Batch vs. concurrent modes&lt;/h3>
&lt;p>&lt;strong>Batch mode&lt;/strong>: Commands run &lt;em>sequentially&lt;/em>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">command&lt;/span> 1&lt;span class="p">;&lt;/span> &lt;span class="nb">command&lt;/span> &lt;span class="m">2&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Command 2 only runs after command 1 is completed.&lt;/p>
&lt;p>&lt;strong>Concurrent mode&lt;/strong>: Commands run in &lt;em>parallel&lt;/em>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">command&lt;/span> &lt;span class="m">1&lt;/span> &lt;span class="p">&amp;amp;&lt;/span> &lt;span class="nb">command&lt;/span> &lt;span class="m">2&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The ampersand operator, after command 1, directs command 1 to operate in the background and passes control to command 2 in the foreground.&lt;/p>
&lt;h2 id="advanced-bash-scripting">Advanced Bash Scripting&lt;/h2>
&lt;h3 id="conditionals">Conditionals&lt;/h3>
&lt;p>Bash script conditionals use the following &lt;code>if&lt;/code>-&lt;code>then&lt;/code>-&lt;code>else&lt;/code> syntax:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="k">if&lt;/span> &lt;span class="o">[&lt;/span> condition &lt;span class="o">]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">then&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> statement_block_1
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">else&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> statement_block_2
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">fi&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="flex px-4 py-3 mb-6 rounded-md bg-primary-100 dark:bg-primary-900">
&lt;span class="pr-3 pt-1 text-primary-600 dark:text-primary-300">
&lt;svg height="24" viewBox="0 0 370 391" xmlns="http://www.w3.org/2000/svg">&lt;g clip-rule="evenodd" fill-rule="evenodd">&lt;path d="m207.5 22.4 114.4 66.6c13.5 7.9 21.9 22.4 21.9 38v136.4c0 17.3-9.3 33.3-24.5 41.8l-113.5 63.9a49.06 49.06 0 0 1 -48.5-.2l-104.5-60.1c-16.4-9.5-26.6-27-26.6-45.9v-129.5c0-19.1 9.9-36.8 26.1-46.8l102.8-63.5c16-9.9 36.2-10.1 52.4-.7z" fill="#ff4088" stroke="#c9177e" stroke-width="27" />&lt;path d="m105.6 298.2v-207.2h43.4v75.5h71.9v-75.5h43.5v207.2h-43.5v-90.6h-71.9v90.6z" fill="#fff" />&lt;/g>&lt;/svg>
&lt;/span>
&lt;span class="dark:text-neutral-300">&lt;ul>
&lt;li>You must always put spaces around your condition within the square brackets &lt;code>[&lt;/code> &lt;code>]&lt;/code>.&lt;/li>
&lt;li>Every &lt;code>if&lt;/code> condition block must be paired with a &lt;code>fi&lt;/code> to tell Bash where the condition block ends.&lt;/li>
&lt;li>The &lt;code>else&lt;/code> block is optional but recommended. If the condition evaluates to &lt;code>false&lt;/code> without an &lt;code>else&lt;/code> block, then nothing happens within the &lt;code>if&lt;/code> condition block. Consider options such as echoing a comment in &lt;code>statement_block_2&lt;/code> to indicate that the condition was evaluated as &lt;code>false&lt;/code>.&lt;/li>
&lt;/ul>
&lt;/span>
&lt;/div>
&lt;p>Example: the condition is checking whether the number of command-line arguments read by some Bash script, &lt;code>$#&lt;/code>, is equal to 2.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="k">if&lt;/span> &lt;span class="o">[[&lt;/span> &lt;span class="nv">$#&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="m">2&lt;/span> &lt;span class="o">]]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">then&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;number of arguments is equal to 2&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">else&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;number of arguments is not equal to 2&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">fi&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Notice: the use of the double square brackets, which is the syntax required for making integer comparisons in the condition &lt;code>[[ $# == 2 ]]&lt;/code>.&lt;/p>
&lt;div class="flex px-4 py-3 mb-6 rounded-md bg-primary-100 dark:bg-primary-900">
&lt;span class="pr-3 pt-1 text-primary-600 dark:text-primary-300">
&lt;svg height="24" viewBox="0 0 370 391" xmlns="http://www.w3.org/2000/svg">&lt;g clip-rule="evenodd" fill-rule="evenodd">&lt;path d="m207.5 22.4 114.4 66.6c13.5 7.9 21.9 22.4 21.9 38v136.4c0 17.3-9.3 33.3-24.5 41.8l-113.5 63.9a49.06 49.06 0 0 1 -48.5-.2l-104.5-60.1c-16.4-9.5-26.6-27-26.6-45.9v-129.5c0-19.1 9.9-36.8 26.1-46.8l102.8-63.5c16-9.9 36.2-10.1 52.4-.7z" fill="#ff4088" stroke="#c9177e" stroke-width="27" />&lt;path d="m105.6 298.2v-207.2h43.4v75.5h71.9v-75.5h43.5v207.2h-43.5v-90.6h-71.9v90.6z" fill="#fff" />&lt;/g>&lt;/svg>
&lt;/span>
&lt;span class="dark:text-neutral-300">&lt;p>&lt;code>$&lt;/code> can be used to access command-line arguments passed to the script.&lt;/p>
&lt;p>Examples:&lt;/p>
&lt;ul>
&lt;li>&lt;code>$0&lt;/code>: The name of the script.&lt;/li>
&lt;li>&lt;code>$1&lt;/code>, &lt;code>$2&lt;/code>, &lt;code>$3&lt;/code>, &amp;hellip;: The first, second, third, etc., arguments passed to the script.&lt;/li>
&lt;li>&lt;code>$#&lt;/code>: The number of arguments passed.&lt;/li>
&lt;li>&lt;code>$@&lt;/code> or &lt;code>$*&lt;/code>: All arguments as a list.&lt;/li>
&lt;li>&lt;code>$?&lt;/code>: The exit status of the last command.&lt;/li>
&lt;li>&lt;code>$$&lt;/code>: The process ID (PID) of the current script.&lt;/li>
&lt;/ul>
&lt;/span>
&lt;/div>
&lt;p>You can also make string comparisons. For example, assume you have a variable called &lt;code>string_var&lt;/code> that has the value &lt;code>&amp;quot;Yes&amp;quot;&lt;/code> assigned to it. Then the following statement evaluates to &lt;code>true&lt;/code>:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="sb">`&lt;/span>&lt;span class="o">[&lt;/span> &lt;span class="nv">$string_var&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="s2">&amp;#34;Yes&amp;#34;&lt;/span> &lt;span class="o">]&lt;/span>&lt;span class="sb">`&lt;/span> &lt;span class="c1"># Only need single square brackets for string comparison&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>You can also include multiple conditions to be satified by using the &lt;strong>&amp;ldquo;and&amp;rdquo; operator &lt;code>&amp;amp;&amp;amp;&lt;/code>&lt;/strong> or the &amp;ldquo;or&amp;rdquo; operator &lt;code>||&lt;/code>. Example&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="k">if&lt;/span> &lt;span class="o">[&lt;/span> condition1 &lt;span class="o">]&lt;/span> &lt;span class="o">&amp;amp;&amp;amp;&lt;/span> &lt;span class="o">[&lt;/span> condition2 &lt;span class="o">]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">then&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;conditions 1 and 2 are both true&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">else&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;one or both conditions are false&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">fi&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">if&lt;/span> &lt;span class="o">[&lt;/span> condition1 &lt;span class="o">]&lt;/span> &lt;span class="o">||&lt;/span> &lt;span class="o">[&lt;/span> condition2 &lt;span class="o">]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">then&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;conditions 1 or 2 are true&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">else&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;both conditions are false&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">fi&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;code>if&lt;/code>-&lt;code>then&lt;/code>-&lt;code>elif&lt;/code>-&lt;code>else&lt;/code> Syntax:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="k">if&lt;/span> &lt;span class="o">[&lt;/span> condition &lt;span class="o">]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">then&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> statement_block_1
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">elif&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> statement_block_2
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">else&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> statement_block_3
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">fi&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="logical-operators">Logical Operators&lt;/h3>
&lt;ul>
&lt;li>
&lt;p>&lt;code>==&lt;/code>: is equal to&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># If a variable a has a value of 2, the following condition evaluates to true; otherwise it evalutes to false.&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nv">$a&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="m">2&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;/li>
&lt;li>
&lt;p>&lt;code>!=&lt;/code>: is not equal to&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;code>&amp;lt;=&lt;/code> or &lt;code>-le&lt;/code>: is less than or equal to&lt;/p>
&lt;/li>
&lt;/ul>
&lt;p>More see: &lt;a href="https://tldp.org/LDP/abs/html/comparison-ops.html">Advanced Bash-Scripting Guide&lt;/a>&lt;/p>
&lt;h3 id="arithmetic-calculations">Arithmetic calculations&lt;/h3>
&lt;p>You can perform integer addition, subtraction, multiplication, and division using the notation &lt;code>$(())&lt;/code>.&lt;/p>
&lt;p>Example&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="k">$((&lt;/span>&lt;span class="m">3&lt;/span>&lt;span class="o">+&lt;/span>&lt;span class="m">2&lt;/span>&lt;span class="k">))&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nv">a&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="m">3&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nv">b&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="m">2&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nv">c&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="k">$((&lt;/span>&lt;span class="nv">$a&lt;/span>&lt;span class="o">+&lt;/span>&lt;span class="nv">$b&lt;/span>&lt;span class="k">))&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="nv">$c&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="flex px-4 py-3 mb-6 rounded-md bg-yellow-100 dark:bg-yellow-900">
&lt;span class="pr-3 pt-1 text-red-400">
&lt;svg height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">&lt;path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0zM12 15.75h.007v.008H12z"/>&lt;/svg>
&lt;/span>
&lt;span class="dark:text-neutral-300">Bash natively handles integer arithmetic but does not handle floating-point arithmetic. As a result, it will always truncate the decimal portion of a calculation result.&lt;/span>
&lt;/div>
&lt;p>Basic arithmetic operators:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Symbol&lt;/th>
&lt;th>Operation&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>&lt;code>+&lt;/code>&lt;/td>
&lt;td>addition&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>-&lt;/code>&lt;/td>
&lt;td>subtraction&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>*&lt;/code>&lt;/td>
&lt;td>multiplication&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>/&lt;/code>&lt;/td>
&lt;td>division&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;h3 id="arrays">Arrays&lt;/h3>
&lt;p>An array is a space-delimited list contained in parentheses. To create an array, declare its name and contents:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nv">my_array&lt;/span>&lt;span class="o">=(&lt;/span>&lt;span class="m">1&lt;/span> &lt;span class="m">2&lt;/span> &lt;span class="s2">&amp;#34;three&amp;#34;&lt;/span> &lt;span class="s2">&amp;#34;four&amp;#34;&lt;/span> 5&lt;span class="o">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>If you want to add items to your array after creating it, you can add to your array by appending one element at a time:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nv">my_array&lt;/span>&lt;span class="o">+=(&lt;/span>&lt;span class="s2">&amp;#34;six&amp;#34;&lt;/span>&lt;span class="o">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nv">my_array&lt;/span>&lt;span class="o">+=(&lt;/span>7&lt;span class="o">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This adds elements &lt;code>&amp;quot;six&amp;quot;&lt;/code> and &lt;code>7&lt;/code> to the array &lt;code>my_array&lt;/code>.&lt;/p>
&lt;p>By using indexing, you can access individual or multiple elements of an array:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># print the first item of the array:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="si">${&lt;/span>&lt;span class="nv">my_array&lt;/span>&lt;span class="p">[0]&lt;/span>&lt;span class="si">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># print the third item of the array:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="si">${&lt;/span>&lt;span class="nv">my_array&lt;/span>&lt;span class="p">[2]&lt;/span>&lt;span class="si">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># print all array elements:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="si">${&lt;/span>&lt;span class="nv">my_array&lt;/span>&lt;span class="p">[@]&lt;/span>&lt;span class="si">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Get the array length:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nv">arr_len&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="si">${#&lt;/span>&lt;span class="nv">arr&lt;/span>&lt;span class="p">[@]&lt;/span>&lt;span class="si">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>or&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nv">arr_len&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="si">${#&lt;/span>&lt;span class="nv">arr&lt;/span>&lt;span class="p">[*]&lt;/span>&lt;span class="si">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Here &lt;code>#&lt;/code> gives the lenght of the array (or string).&lt;/p>
&lt;h3 id="for-loops">&lt;code>for&lt;/code> loops&lt;/h3>
&lt;p>You can use a &lt;code>for&lt;/code> loop along with indexing to iterate over all elements of an array.&lt;/p>
&lt;p>Syntax&lt;/p>
&lt;ul>
&lt;li>The &lt;code>for&lt;/code> loop requires a &lt;code>; do&lt;/code> component in order to cycle through the loop.&lt;/li>
&lt;li>You need to terminate the &lt;code>for&lt;/code> loop block with a &lt;code>done&lt;/code> statement.&lt;/li>
&lt;/ul>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="k">for&lt;/span> item in &lt;span class="si">${&lt;/span>&lt;span class="nv">arr&lt;/span>&lt;span class="p">[@]&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="p">;&lt;/span> &lt;span class="k">do&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1"># Handle $item&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">done&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="k">for&lt;/span> i in &lt;span class="si">${&lt;/span>&lt;span class="p">!arr[@]&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="p">;&lt;/span> &lt;span class="k">do&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1"># Handle ${arr[$i]}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">done&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Another way to implement a &lt;code>for&lt;/code> loop when you know how many iterations you want is as follows. For example, the following code prints the number 0 through 6.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="nv">N&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="m">6&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">for&lt;/span> &lt;span class="o">((&lt;/span> &lt;span class="nv">i&lt;/span>&lt;span class="o">=&lt;/span>0&lt;span class="p">;&lt;/span> i&amp;lt;&lt;span class="o">=&lt;/span>&lt;span class="nv">$N&lt;/span>&lt;span class="p">;&lt;/span> i++ &lt;span class="o">))&lt;/span> &lt;span class="p">;&lt;/span> &lt;span class="k">do&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nb">echo&lt;/span> &lt;span class="nv">$i&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">done&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="scheduling-jobs-using-cron">Scheduling Jobs Using Cron&lt;/h2>
&lt;p>Job scheduling: Schedule jobs to run automatically at certain times. E.g., &amp;ldquo;Backup script to run every Sunday at 2AM&amp;rdquo;.&lt;/p>
&lt;p>&lt;strong>Cron&lt;/strong> is the general name of the tool that runs scheduled jobs consisting of shell commands or shell scripts.&lt;/p>
&lt;p>&lt;strong>Crond&lt;/strong> is the daemon or service that interprets “crontab files” every minute and submits the corresponding jobs to cron at scheduled&lt;/p>
&lt;p>times.&lt;/p>
&lt;p>A &lt;strong>crontab&lt;/strong>, short for “cron table,” is a simple text file containing jobs and schedule data. Crontab is also a command that invokes a text editor to allow you to edit a crontab file.&lt;/p>
&lt;h4 id="adding-jobs-in-crontab">Adding jobs in Crontab&lt;/h4>
&lt;p>Enter &lt;code>crontab -e&lt;/code> opens the default text editor and allows you to specify a new schedule and a new command.&lt;/p>
&lt;p>Syntax:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">m h dom mon dow &lt;span class="nb">command&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Five time-and-date fields &lt;code>m h dom mon dow&lt;/code> stand for: minute, hour, day of month, month, and day of week. All five positions must have either a numeric entry or an asterisk (&lt;code>*&lt;/code>), which is a wildcard symbol that means “any.”&lt;/p>
&lt;p>The five time-and-date fields cannot contain spaces and their allowed values are as follows:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Field&lt;/th>
&lt;th>Allowed values&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>minute&lt;/td>
&lt;td>0-59&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>hour&lt;/td>
&lt;td>0-23, 0 = midnight&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>day&lt;/td>
&lt;td>1-31&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>month&lt;/td>
&lt;td>1-12&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>weekday&lt;/td>
&lt;td>0-6, 0 = Sunday&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>Example: Append the current date to the file ‘sundays.txt’ at 15:30 every Sunday.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="m">30&lt;/span> &lt;span class="m">15&lt;/span> * * &lt;span class="m">0&lt;/span> date &amp;gt;&amp;gt; sundays.txt
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Then save and exit the editor.&lt;/p>
&lt;h4 id="viewing-and-removing-cron-jobs">Viewing and removing Cron jobs&lt;/h4>
&lt;p>Viewing: &lt;code>crontab -l&lt;/code>&lt;/p>
&lt;p>Removing&lt;/p>
&lt;ul>
&lt;li>Use &lt;code>crontab -e&lt;/code> to open the editor and remove specific line(s)&lt;/li>
&lt;li>&lt;code>crontabl r&lt;/code>: Remove all cron jobs&lt;/li>
&lt;/ul></description></item></channel></rss>