<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Marcus &#8211; MarcTek Tutorials</title>
	<atom:link href="https://tutorials.marctek.com/author/marcus/feed/" rel="self" type="application/rss+xml" />
	<link>https://tutorials.marctek.com</link>
	<description>Build While You Learn</description>
	<lastBuildDate>Sun, 26 Jul 2026 00:10:45 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.2</generator>

<image>
	<url>https://tutorials.marctek.com/wp-content/uploads/2026/07/cropped-MarcTek-Icon-32x32.png</url>
	<title>Marcus &#8211; MarcTek Tutorials</title>
	<link>https://tutorials.marctek.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Script in Bash</title>
		<link>https://tutorials.marctek.com/bash/how-to-script-in-bash/</link>
		
		<dc:creator><![CDATA[Marcus]]></dc:creator>
		<pubDate>Sat, 25 Jul 2026 01:55:09 +0000</pubDate>
				<category><![CDATA[Bash]]></category>
		<guid isPermaLink="false">https://tutorials.marctek.com/?p=40</guid>

					<description><![CDATA[Table of Contents: What is Bash? Bash is a command-line shell and scripting language that is widely used in Unix-like [&#8230;]]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">Table of Contents:</h2>



<ul class="wp-block-list">
<li><a href="#what-is-bash">What is Bash?</a></li>



<li><a href="#bash-commands">Bash Commands</a></li>



<li><a href="#scripting-in-bash">Scripting in Bash</a>
<ul class="wp-block-list">
<li><a href="#variables">Variables</a></li>



<li><a href="#conditionals">Conditionals</a></li>



<li><a href="#loops">Loops</a></li>



<li><a href="#arrays">Arrays</a></li>



<li><a href="#functions">Functions</a></li>
</ul>
</li>
</ul>



<h2 id="what-is-bash" class="wp-block-heading">What is Bash?</h2>



<p class="wp-block-paragraph">Bash is a command-line shell and scripting language that is widely used in Unix-like operating systems such as Linux and macOS. Bash stands for &#8220;Bourne Again SHell,&#8221; a play on the earlier &#8220;Bourne Shell&#8221; it was designed to replace. Bash was created by Brian Fox in 1989 while working at the Free Software Foundation. He developed it as a free software replacement for the Bourne shell (sh), adding more features like command history, better scripting capabilities, and improved user interaction. Later, Chet Ramey became the primary maintainer and has overseen Bash&#8217;s development for decades, ensuring it remains stable and widely used across Linux and Unix-like systems.</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p class="wp-block-paragraph"><strong>Note:</strong></p>



<p class="wp-block-paragraph">In some examples, you&#8217;ll see a <code>$</code> at the beginning of a command. This symbol indicates that the command should be run in a terminal. You don&#8217;t type the <code>$</code> itself, only the text that follows it.</p>
</blockquote>



<p class="wp-block-paragraph">To use bash, you need to confirm it’s installed on your system. Run:</p>



<pre class="wp-block-code"><code>$ bash --version</code></pre>



<p class="wp-block-paragraph">If you see output like <code>GNU bash, version X.X.X(X)-release</code>, bash is installed. If instead you get an error, bash isn’t available on your device, and you won’t be able to use it there.</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p class="wp-block-paragraph"><strong>Note</strong>:</p>



<p class="wp-block-paragraph">In Bash, the <code>#</code> symbol is used to mark a comment. Everything written after <code>#</code> on the same line is ignored by the shell. Comments are helpful notes added to code to explain what it does, making programs easier to read and maintain.</p>



<pre class="wp-block-code"><code># This is a comment!</code></pre>
</blockquote>



<h2 id="bash-commands" class="wp-block-heading">Bash Commands</h2>



<p class="wp-block-paragraph">In Bash, <em>commands</em> are instructions you type into the shell to make the computer do something. They can be built-in functions of the shell, external programs, or scripts you write yourself.</p>



<p class="wp-block-paragraph">Bash commands usually follow this syntax:</p>



<pre class="wp-block-code"><code>command options/flags arguments</code></pre>



<p class="wp-block-paragraph">For example:</p>



<pre class="wp-block-code"><code>$ ls -l /etc</code></pre>



<p class="wp-block-paragraph"><code>ls</code> is the command, it stands for &#8220;list&#8221;. It lists the files and folders in a directory.</p>



<p class="wp-block-paragraph"><code>-l</code> is an option/flag. It displays detailed information of those files and folders. Some commands don&#8217;t require options (or flags), so you can pass the argument directly without options.</p>



<p class="wp-block-paragraph"><code>/etc</code> is the argument, it is a path to a directory. It tells <code>ls</code> to &#8220;list&#8221; all the files and folders in the <code>/etc</code> directory.</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p class="wp-block-paragraph"><strong>Note</strong>:</p>



<p class="wp-block-paragraph">A <em>path</em> is like an address that tells the shell where a file or directory is located. There are two main types of paths:</p>



<p class="wp-block-paragraph"><strong>Absolute Paths</strong>:</p>



<p class="wp-block-paragraph">Absolute Paths always start from the root directory <code>/</code>. They point to the same location regardless of your current working directory. Here is an example of an absolute path: <code>/usr/bin/env</code>. This always refers to the <code>env</code> executable.</p>



<p class="wp-block-paragraph"><strong>Relative Paths</strong>:</p>



<p class="wp-block-paragraph">Relative Paths start from your current working directory. For example: <code>Downloads/file.txt</code>. It refers to <code>file.txt</code> inside <code>Downloads</code>, but only if <code>Downloads</code> exists in your current directory. Special symbols can change the starting point:</p>



<pre class="wp-block-code"><code>.   # Refers to the current directory
..  # Refers to the parent directory
~   # Refers to the home directory of the current user

# Usage:

./file.txt    # Points to file.txt in the current directory

../file2.txt  # Points to file2.txt in the parent directory

../../test/   # Points to the test directory two levels up

~/Downloads   # Points to the Downloads folder in the user's home directory</code></pre>
</blockquote>



<p class="wp-block-paragraph">Here are some common, built-in Bash commands:</p>



<ul class="wp-block-list">
<li><code>cat [options/flags (optional)] [arguments]</code>
<ul class="wp-block-list">
<li><code>cat</code> stands for &#8220;Concatenate.&#8221; It displays the contents of files or merges multiple files together.</li>
</ul>
</li>
</ul>



<p class="wp-block-paragraph"></p>



<ul class="wp-block-list">
<li><code>cd [options/flags (optional)] [path]</code>
<ul class="wp-block-list">
<li><code>cd</code> stands for &#8220;Change Directory.&#8221; It moves you into the specified directory (default is current directory).</li>
</ul>
</li>
</ul>



<p class="wp-block-paragraph"></p>



<ul class="wp-block-list">
<li><code>echo [options/flags (optional)] [text]</code>
<ul class="wp-block-list">
<li><code>echo</code> is used to display messages or variable values.</li>
</ul>
</li>
</ul>



<p class="wp-block-paragraph"></p>



<ul class="wp-block-list">
<li><code>exit [status code (optional)]</code>
<ul class="wp-block-list">
<li><code>exit</code> ends the current shell session or script. The status code <code>0</code> means success; <code>1–255</code> indicate errors.</li>
</ul>
</li>
</ul>



<p class="wp-block-paragraph"></p>



<ul class="wp-block-list">
<li><code>help [options/flags (optional)] [command (optional)]</code>
<ul class="wp-block-list">
<li><code>help</code> shows information about built-in Bash commands, including available options and arguments.</li>
</ul>
</li>
</ul>



<p class="wp-block-paragraph"></p>



<ul class="wp-block-list">
<li><code>logout [status code (optional)]</code>
<ul class="wp-block-list">
<li><code>logout</code> logs you out of your current user session (with a status code).</li>
</ul>
</li>
</ul>



<p class="wp-block-paragraph"></p>



<ul class="wp-block-list">
<li><code>ls [options/flags (optional)] [path]</code>
<ul class="wp-block-list">
<li><code>ls</code> stands for &#8220;List.&#8221; It displays files and directories in the given path (default is current directory).</li>
</ul>
</li>
</ul>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">There are many more commands available. Use help or check your operating system&#8217;s documentation to explore them further.</p>



<h2 id="scripting-in-bash" class="wp-block-heading">Scripting in Bash</h2>



<p class="wp-block-paragraph">A Bash script is simply a text file containing a series of Bash commands that the shell can execute in order, rather than you typing them one by one in the terminal.</p>



<p class="wp-block-paragraph">Bash scripts are very easy to write. To create one, start by making a file with the <code>.sh</code><strong> </strong>extension. Directly at the top of the file, it’s best practice to include a shebang:</p>



<pre class="wp-block-code"><code>#!/usr/bin/env bash</code></pre>



<p class="wp-block-paragraph">The shebang (<code>#!</code>) at the start of a script tells the system which interpreter to use. An interpreter is a foundational programming tool that executes written code. In this case, <code>#!/usr/bin/env bash</code> locates the <code>bash</code> interpreter via the <code>env</code> command and runs the script with it.</p>



<p class="wp-block-paragraph">Once you&#8217;ve finished writing a Bash script, the next step is to run it on your computer. Before you can execute the script, you need to give it permission to be executable:</p>



<pre class="wp-block-code"><code>$ chmod +x script.sh</code></pre>



<p class="wp-block-paragraph">Replace <code>script.sh</code> with the name of your script. Once it’s executable, you can run it in different ways:</p>



<pre class="wp-block-code"><code>$ bash script.sh # Executes the script using bash

$ sh script.sh   # Executes the script using sh (also invokes bash in most systems)

$ ./script.sh    # Runs the script directly, but requires a proper shebang at the top</code></pre>



<h3 id="variables" class="wp-block-heading">Variables</h3>



<p class="wp-block-paragraph">A variable is like a labeled container that holds information. Imagine you have a box with a sticky note on it that says &#8220;greeting.&#8221; Inside the box, you can put anything: a word, a number, or even the result of a command. Later, you can open the box and use whatever is inside without rewriting it.</p>



<p class="wp-block-paragraph">Instead of repeating the same value over and over, you give it a &#8220;nickname&#8221; (the variable name) and reuse it.</p>



<p class="wp-block-paragraph">The basic format of a variable in Bash is:</p>



<pre class="wp-block-code"><code>variable_name=value</code></pre>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p class="has-luminous-vivid-amber-color has-text-color has-link-color wp-elements-81ba6fe8ae24092910f860a088e27231 wp-block-paragraph"><strong>Important</strong>:</p>



<p class="wp-block-paragraph">There should be no spaces around the <code>=</code> sign.</p>
</blockquote>



<p class="wp-block-paragraph">Here is an example of storing &#8220;Hello!&#8221; into the variable <code>greeting</code>:</p>



<pre class="wp-block-code"><code>greeting="Hello!"</code></pre>



<blockquote class="wp-block-quote is-style-default is-layout-flow wp-block-quote-is-layout-flow">
<p class="wp-block-paragraph"><strong>Note</strong>:</p>



<p class="wp-block-paragraph">In Bash, variables are not strongly typed, meaning Bash doesn&#8217;t enforce strict data types like integers or floats. Instead, they are treated as strings by default, but you can use them in different contexts (like arithmetic or environment variables).</p>
</blockquote>



<p class="wp-block-paragraph">To use the value inside a variable, put a <code>$</code> before its name:</p>



<pre class="wp-block-code"><code>echo $greeting</code></pre>



<p class="wp-block-paragraph">If you execute the script, it should return <code>Hello!</code> in the console. Now replace &#8220;Hello!&#8221; in the greeting variable to <code>5</code>:</p>



<pre class="wp-block-code"><code>greeting=5</code></pre>



<p class="wp-block-paragraph">If you execute this, it will output <code>5</code>.</p>



<p class="wp-block-paragraph">Bash runs scripts line by line from top to bottom. That means the order matters. So if you write something like:</p>



<pre class="wp-block-code"><code>greeting="Hello!"
echo $greeting
greeting=5</code></pre>



<p class="wp-block-paragraph">It will output <code>Hello!</code> because <code>echo</code> happened before <code>greeting</code> was changed.</p>



<p class="wp-block-paragraph">You can do arithmetic on variables using operators. Operators are symbols or keywords that let you perform actions on values or variables. Here is a list of all the operators:</p>


<div class="kb-table-container kb-table-container40_6ffb19-bf wp-block-kadence-table"><table class="kb-table kb-table40_6ffb19-bf">
<tr class="kb-table-row kb-table-row40_f713ef-08">
<td  class="kb-table-data kb-table-data40_f7034b-a6">

<p class="has-text-align-center wp-block-paragraph"><strong>Operators</strong></p>

</td>

<td  class="kb-table-data kb-table-data40_984dfc-c3">

<p class="has-text-align-center wp-block-paragraph"><strong>Math</strong></p>

</td>
</tr>

<tr class="kb-table-row kb-table-row40_f1af8b-16">
<td  class="kb-table-data kb-table-data40_24e6a9-7d">

<p class="has-text-align-center wp-block-paragraph">*</p>

</td>

<td  class="kb-table-data kb-table-data40_f72cd1-4b">

<p class="has-text-align-center wp-block-paragraph">Multiplication</p>

</td>
</tr>

<tr class="kb-table-row kb-table-row40_cd35ec-e3">
<td  class="kb-table-data kb-table-data40_b69492-17">

<p class="has-text-align-center wp-block-paragraph">/</p>

</td>

<td  class="kb-table-data kb-table-data40_e54c4e-6c">

<p class="has-text-align-center wp-block-paragraph">Division</p>

</td>
</tr>

<tr class="kb-table-row kb-table-row40_67e056-52">
<td  class="kb-table-data kb-table-data40_787e7c-36">

<p class="has-text-align-center wp-block-paragraph">+</p>

</td>

<td  class="kb-table-data kb-table-data40_66b750-f4">

<p class="has-text-align-center wp-block-paragraph">Addition</p>

</td>
</tr>

<tr class="kb-table-row kb-table-row40_89b62f-a4">
<td  class="kb-table-data kb-table-data40_951037-05">

<p class="has-text-align-center wp-block-paragraph">&#8211;</p>

</td>

<td  class="kb-table-data kb-table-data40_3e2501-8c">

<p class="has-text-align-center wp-block-paragraph">Subtraction</p>

</td>
</tr>

<tr class="kb-table-row kb-table-row40_cc0d55-30">
<td  class="kb-table-data kb-table-data40_fe8f1c-38">

<p class="has-text-align-center wp-block-paragraph">**</p>

</td>

<td  class="kb-table-data kb-table-data40_852fdb-b6">

<p class="has-text-align-center wp-block-paragraph">Power To</p>

</td>
</tr>

<tr class="kb-table-row kb-table-row40_8e579c-1a">
<td  class="kb-table-data kb-table-data40_e62db7-37">

<p class="has-text-align-center wp-block-paragraph">&amp;</p>

</td>

<td  class="kb-table-data kb-table-data40_93f5fb-fc">

<p class="has-text-align-center wp-block-paragraph">Bitwise AND</p>

</td>
</tr>

<tr class="kb-table-row kb-table-row40_748b83-85">
<td  class="kb-table-data kb-table-data40_936f52-d4">

<p class="has-text-align-center wp-block-paragraph">|</p>

</td>

<td  class="kb-table-data kb-table-data40_56f2af-dc">

<p class="has-text-align-center wp-block-paragraph">Bitwise OR</p>

</td>
</tr>

<tr class="kb-table-row kb-table-row40_268c7a-fe">
<td  class="kb-table-data kb-table-data40_fec9f2-9d">

<p class="has-text-align-center wp-block-paragraph">^</p>

</td>

<td  class="kb-table-data kb-table-data40_f7183e-ef">

<p class="has-text-align-center wp-block-paragraph">Bitwise XOR</p>

</td>
</tr>

<tr class="kb-table-row kb-table-row40_c2db1f-b4">
<td  class="kb-table-data kb-table-data40_a43ff2-5d">

<p class="has-text-align-center wp-block-paragraph">~</p>

</td>

<td  class="kb-table-data kb-table-data40_934c4e-ba">

<p class="has-text-align-center wp-block-paragraph">Bitwise NOT</p>

</td>
</tr>

<tr class="kb-table-row kb-table-row40_998b52-42">
<td  class="kb-table-data kb-table-data40_e042ef-32">

<p class="has-text-align-center wp-block-paragraph">&lt;&lt;</p>

</td>

<td  class="kb-table-data kb-table-data40_4111b0-32">

<p class="has-text-align-center wp-block-paragraph">Left Shift</p>

</td>
</tr>

<tr class="kb-table-row kb-table-row40_4eb15c-1c">
<td  class="kb-table-data kb-table-data40_1b44eb-1a">

<p class="has-text-align-center wp-block-paragraph">>></p>

</td>

<td  class="kb-table-data kb-table-data40_e7a606-3d">

<p class="has-text-align-center wp-block-paragraph">Right Shift</p>

</td>
</tr>
</table></div>


<p class="wp-block-paragraph">To do math, wrap your expression in double parentheses <code>(( ))</code>. Inside <code>(( ))</code>, you don’t need <code>$</code> before variable names. Here is an example:</p>



<pre class="wp-block-code"><code>x=10
y=2

(( z = 5 * 5 ))    # Creates the variable z and sets it to 5 times 5 (5 * 5 = 25)

(( z = x / y )) # Reassigns z (since it was created before) with x (10) divided by y (2) (10 / 2 = 5)

echo $z # Outputs 5</code></pre>



<p class="wp-block-paragraph">If you execute this script it will output the number <code>5</code>.</p>



<p class="wp-block-paragraph">You can store the result of a command inside a variable using <code>$( )</code>. This is the format:</p>



<pre class="wp-block-code"><code>variable_name=$(command options/flags arguments)</code></pre>



<p class="wp-block-paragraph">Here is an example:</p>



<pre class="wp-block-code"><code>cores=$(nproc)
echo "You have $cores cores available."</code></pre>



<p class="wp-block-paragraph"><code>nproc</code> is a command that returns the number of logical CPUs available to the current process. The script runs <code>nproc</code>, stores the returned number in the variable <code>cores</code>, and then echoes something like: &#8220;You have 8 cores available.&#8221; (The actual number depends on your computer).</p>



<p class="wp-block-paragraph">Bash also has environment variables, special variables that affect how programs run. Here is a list of common environment variables:</p>


<div class="kb-table-container kb-table-container40_ca9c20-b8 wp-block-kadence-table"><table class="kb-table kb-table40_ca9c20-b8">
<tr class="kb-table-row kb-table-row40_92b5f1-2b">
<td  class="kb-table-data kb-table-data40_ff8d4e-e5">

<p class="has-text-align-center wp-block-paragraph"><strong>Environment Variables</strong></p>

</td>

<td  class="kb-table-data kb-table-data40_c6c543-0f">

<p class="has-text-align-center wp-block-paragraph"><strong>Stores</strong></p>

</td>
</tr>

<tr class="kb-table-row kb-table-row40_f60ba6-20">
<td  class="kb-table-data kb-table-data40_48ce59-43">

<p class="has-text-align-center wp-block-paragraph">$HOME</p>

</td>

<td  class="kb-table-data kb-table-data40_949ec1-77">

<p class="has-text-align-center wp-block-paragraph">The absolute path to your home directory.</p>

</td>
</tr>

<tr class="kb-table-row kb-table-row40_44e018-0c">
<td  class="kb-table-data kb-table-data40_388952-1a">

<p class="has-text-align-center wp-block-paragraph">$PATH</p>

</td>

<td  class="kb-table-data kb-table-data40_9a3618-fe">

<p class="has-text-align-center wp-block-paragraph">The list of directories Bash searches for commands.</p>

</td>
</tr>
</table></div>


<h3 class="wp-block-heading">Conditionals</h3>



<p class="wp-block-paragraph">Conditional structures in scripting are rules that let your code make decisions. They check whether something is <em>true or false</em> and then choose which block of code to run. In Bash, conditional structures use numeric comparison operators (for numbers), string comparison operators (for strings), file test operators (for files and directories), and logical operators. You can find the whole list of operators here.</p>



<p class="wp-block-paragraph">If-Else statements in Bash are conditional structures. Here is the syntax:</p>



<pre class="wp-block-code"><code>if &#91;&#91; operand operator operand ]]; then
  ...
elif &#91;&#91; ... ]]; then
  ...
else
  ...
fi</code></pre>



<p class="wp-block-paragraph">An <code>operand</code> can be a variable, a number, or any value to be tested. An <code>operator</code> is the comparison or test itself (e.g., <code>-eq</code>, <code>-gt</code>, <code>==</code>). The condition compares the two operands using the operator and evaluates whether it is true.</p>



<p class="wp-block-paragraph">If the initial <code>if</code> condition is true, its block executes. If it is false, Bash moves on to the next condition specified by <code>elif</code>. The keyword <code>elif</code> stands for &#8220;else if&#8221; and is only checked when the previous <code>if</code> (or another <code>elif</code>) fails. You can include multiple <code>elif</code> branches in a single if-else chain.</p>



<p class="wp-block-paragraph">If none of the <code>if</code> or <code>elif</code> conditions are true, the <code>else</code> block executes. This acts as the &#8220;catch-all&#8221; case when every other condition fails.</p>



<p class="wp-block-paragraph">Here is an example of an If-Else statement:</p>



<pre class="wp-block-code"><code>x=5
y=4

if &#91;&#91; $x == $y ]]; then
  exit 0 // This won't execute because the if statement above is false.
elif (( x + y = x + y )); then // Using (( )) to do arithmetic on x and y.
  exit 0 // Because the elif is true, this will be executed.
else
  exit 1 // This will never be reached because the elif above is true.
fi
</code></pre>



<p class="wp-block-paragraph">The elif was true so the exit command in there will execute.</p>



<p class="wp-block-paragraph">In Bash, case statements are conditional structures. They match a variable or expression against multiple patterns and execute code depending on which pattern matches. They look and act like switch statements in other programming languages. This is the syntax:</p>



<pre class="wp-block-code"><code>case expression in
  pattern1)
    ...
    ;;
  pattern2)
    ...
    ;;
  pattern3|pattern4)
    ...
    ;;
  *)
    ...
    ;;
esac</code></pre>



<p class="wp-block-paragraph">The <code>expression</code> could be a variable or just an expression with operators. The <code>patternN</code> are compared to the expression to see if they are equal and execute the code inside. The <code>;;</code> are breaks so Bash can exit the case after the code in the pattern finished. You can use a <code>|</code> in a case block. The <code>|</code> is used to separate multiple patterns that should trigger the same block of code. If no pattern matched, then the code under the <code>*</code> is executed. The <code>*</code> is the default and will only always execute if no pattern was found. The <code>esac</code> just ends the case statement.</p>



<p class="wp-block-paragraph">Here is an example of a case statement:</p>



<pre class="wp-block-code"><code>animals="cows"

case $animals in
  "sheep")
    echo "There are sheep!" // This won't execute because $animals isn't sheep
    ;;
  "cows")
    echo "There are cows!" // This will execute because the pattern is true
    ;;
  *)
    echo "There are animals!"
    ;;
esac</code></pre>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
