<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Malhar Gupte</title>
    <description>The latest articles on DEV Community by Malhar Gupte (@guptem).</description>
    <link>https://dev.to/guptem</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2336759%2Fc0c9f97b-534f-4e02-ac35-320085845291.jpg</url>
      <title>DEV Community: Malhar Gupte</title>
      <link>https://dev.to/guptem</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/guptem"/>
    <language>en</language>
    <item>
      <title>Shell Scripting for Beginners: Control Flow, Functions and Real Automation (Part 2)</title>
      <dc:creator>Malhar Gupte</dc:creator>
      <pubDate>Fri, 13 Mar 2026 05:41:18 +0000</pubDate>
      <link>https://dev.to/guptem/shell-scripting-for-beginners-control-flow-functions-and-real-automation-part-2-3m28</link>
      <guid>https://dev.to/guptem/shell-scripting-for-beginners-control-flow-functions-and-real-automation-part-2-3m28</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Series:&lt;/strong&gt; Shell Scripting for Beginners | &lt;strong&gt;Part:&lt;/strong&gt; 2 of 2&lt;br&gt;
&lt;strong&gt;Level:&lt;/strong&gt; Beginner–Intermediate | &lt;strong&gt;Time to read:&lt;/strong&gt; ~18 minutes&lt;br&gt;
&lt;strong&gt;Part 1:&lt;/strong&gt; &lt;a href="https://dev.to/guptem/shell-scripting-for-beginners-from-zero-to-automating-your-first-tasks-part-1-2of0"&gt;Shell Scripting for Beginners: From Zero to Automating Your First Tasks&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Quick Recap of Part 1
&lt;/h2&gt;

&lt;p&gt;In Part 1, we covered the building blocks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Writing and running your first script (&lt;code&gt;chmod +x&lt;/code&gt;, &lt;code&gt;./script.sh&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;The shebang line (&lt;code&gt;#!/bin/bash&lt;/code&gt;) and what it does&lt;/li&gt;
&lt;li&gt;Variables, curly brace syntax, and command substitution&lt;/li&gt;
&lt;li&gt;Passing arguments with &lt;code&gt;$1&lt;/code&gt;, &lt;code&gt;$2&lt;/code&gt;, &lt;code&gt;$#&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Reading user input with &lt;code&gt;read&lt;/code&gt; and &lt;code&gt;read -p&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Arithmetic using &lt;code&gt;$(( ))&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If any of that sounds unfamiliar, go through Part 1 first — everything in this article builds on it.&lt;/p&gt;

&lt;p&gt;In Part 2, we're adding the logic layer. By the end of this, your scripts will be able to make decisions, repeat tasks, handle errors, and be structured enough that you'd actually want to maintain them.&lt;/p&gt;

&lt;p&gt;Let's get into it.&lt;/p&gt;




&lt;h2&gt;
  
  
  Conditional Logic (if Statements)
&lt;/h2&gt;

&lt;p&gt;A script that just runs commands top to bottom is useful. A script that can &lt;em&gt;decide&lt;/em&gt; what to do based on conditions is powerful.&lt;/p&gt;

&lt;h3&gt;
  
  
  Basic &lt;code&gt;if&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="nv"&gt;age&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;20

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nv"&gt;$age&lt;/span&gt; &lt;span class="nt"&gt;-ge&lt;/span&gt; 18 &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"You're an adult."&lt;/span&gt;
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The structure is always:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; condition &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
  &lt;span class="c"&gt;# commands to run if condition is true&lt;/span&gt;
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The spaces inside &lt;code&gt;[ ]&lt;/code&gt; are &lt;strong&gt;not optional&lt;/strong&gt;. &lt;code&gt;[$age -ge 18]&lt;/code&gt; will fail. Always write &lt;code&gt;[ $age -ge 18 ]&lt;/code&gt; with spaces.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;if-else&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="nb"&gt;read&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; &lt;span class="s2"&gt;"Enter your age: "&lt;/span&gt; age

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nv"&gt;$age&lt;/span&gt; &lt;span class="nt"&gt;-ge&lt;/span&gt; 18 &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Access granted."&lt;/span&gt;
&lt;span class="k"&gt;else
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Access denied. Come back in a few years."&lt;/span&gt;
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;if-elif-else&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="nb"&gt;read&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; &lt;span class="s2"&gt;"Enter your score: "&lt;/span&gt; score

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nv"&gt;$score&lt;/span&gt; &lt;span class="nt"&gt;-ge&lt;/span&gt; 90 &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Grade: A"&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nv"&gt;$score&lt;/span&gt; &lt;span class="nt"&gt;-ge&lt;/span&gt; 75 &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Grade: B"&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nv"&gt;$score&lt;/span&gt; &lt;span class="nt"&gt;-ge&lt;/span&gt; 60 &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Grade: C"&lt;/span&gt;
&lt;span class="k"&gt;else
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Grade: F — study harder."&lt;/span&gt;
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Comparison Operators
&lt;/h3&gt;

&lt;p&gt;Bash uses different operators for numbers and strings — this trips people up constantly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For numbers:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operator&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-eq&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;equal to&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-ne&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;not equal to&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-gt&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;greater than&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-lt&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;less than&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-ge&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;greater than or equal&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-le&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;less than or equal&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;For strings:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operator&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;=&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;equal&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;!=&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;not equal&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-z&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;is empty&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-n&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;is not empty&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Malhar"&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$name&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Malhar"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Hey, I know you."&lt;/span&gt;
&lt;span class="k"&gt;fi

if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nt"&gt;-z&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$name&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Name is empty."&lt;/span&gt;
&lt;span class="k"&gt;else
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Name is: &lt;/span&gt;&lt;span class="nv"&gt;$name&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;For files and directories:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operator&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-f&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;file exists&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-d&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;directory exists&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-e&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;file or directory exists&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-r&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;file is readable&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-w&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;file is writable&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-x&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;file is executable&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; &lt;span class="s2"&gt;"/etc/passwd"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"File exists."&lt;/span&gt;
&lt;span class="k"&gt;fi

if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s2"&gt;"/tmp"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"/tmp directory exists."&lt;/span&gt;
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; Always quote your variables inside &lt;code&gt;[ ]&lt;/code&gt;. Write &lt;code&gt;[ "$name" = "Malhar" ]&lt;/code&gt;, not &lt;code&gt;[ $name = "Malhar" ]&lt;/code&gt;. If &lt;code&gt;$name&lt;/code&gt; is empty or has spaces, the unquoted version breaks.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Double Brackets &lt;code&gt;[[ ]]&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Bash also supports &lt;code&gt;[[ ]]&lt;/code&gt; — a more modern, forgiving syntax that handles edge cases better:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Malhar Gupte"&lt;/span&gt;

&lt;span class="c"&gt;# This works safely with spaces in the variable&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt; &lt;span class="nv"&gt;$name&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="s2"&gt;"Malhar"&lt;/span&gt;&lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="o"&gt;]]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Name contains Malhar"&lt;/span&gt;
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;[[ ]]&lt;/code&gt; also supports &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; and &lt;code&gt;||&lt;/code&gt; directly inside it, while &lt;code&gt;[ ]&lt;/code&gt; doesn't. Once you're comfortable with the basics, prefer &lt;code&gt;[[ ]]&lt;/code&gt; for new scripts.&lt;/p&gt;




&lt;h2&gt;
  
  
  For Loops
&lt;/h2&gt;

&lt;p&gt;When you need to repeat something a fixed number of times or iterate over a list — that's a &lt;code&gt;for&lt;/code&gt; loop.&lt;/p&gt;

&lt;h3&gt;
  
  
  Looping Through a List of Values
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="k"&gt;for &lt;/span&gt;language &lt;span class="k"&gt;in &lt;/span&gt;Python Bash Java Go Rust&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Language: &lt;/span&gt;&lt;span class="nv"&gt;$language&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;Language&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Python&lt;/span&gt;
&lt;span class="n"&gt;Language&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Bash&lt;/span&gt;
&lt;span class="n"&gt;Language&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Java&lt;/span&gt;
&lt;span class="n"&gt;Language&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Go&lt;/span&gt;
&lt;span class="n"&gt;Language&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Rust&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Looping Through a Number Range
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="k"&gt;for &lt;/span&gt;i &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;1..5&lt;span class="o"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Iteration: &lt;/span&gt;&lt;span class="nv"&gt;$i&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or with a step:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;for &lt;/span&gt;i &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;0..20..5&lt;span class="o"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$i&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;   &lt;span class="c"&gt;# 0, 5, 10, 15, 20&lt;/span&gt;
&lt;span class="k"&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  C-Style For Loop
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;((&lt;/span&gt;&lt;span class="nv"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1&lt;span class="p"&gt;;&lt;/span&gt; i&amp;lt;&lt;span class="o"&gt;=&lt;/span&gt;5&lt;span class="p"&gt;;&lt;/span&gt; i++&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Count: &lt;/span&gt;&lt;span class="nv"&gt;$i&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Looping Through Files
&lt;/h3&gt;

&lt;p&gt;This is where &lt;code&gt;for&lt;/code&gt; loops get genuinely useful:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="c"&gt;# Loop through all .log files in a directory&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;file &lt;span class="k"&gt;in&lt;/span&gt; /var/log/&lt;span class="k"&gt;*&lt;/span&gt;.log&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Processing: &lt;/span&gt;&lt;span class="nv"&gt;$file&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Size: &lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;du&lt;/span&gt; &lt;span class="nt"&gt;-sh&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$file&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; | &lt;span class="nb"&gt;cut&lt;/span&gt; &lt;span class="nt"&gt;-f1&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Practical Example: Batch Rename Files
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="c"&gt;# Add a "backup_" prefix to all .txt files in current directory&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;file &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt;.txt&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do
  &lt;/span&gt;&lt;span class="nb"&gt;mv&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$file&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="s2"&gt;"backup_&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;file&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Renamed: &lt;/span&gt;&lt;span class="nv"&gt;$file&lt;/span&gt;&lt;span class="s2"&gt; → backup_&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;file&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; Always test loops with &lt;code&gt;echo&lt;/code&gt; first before running destructive commands like &lt;code&gt;mv&lt;/code&gt;, &lt;code&gt;rm&lt;/code&gt;, or &lt;code&gt;cp&lt;/code&gt;. Replace the actual command with &lt;code&gt;echo "would run: mv $file ..."&lt;/code&gt; and verify the output looks right.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  While Loops
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;while&lt;/code&gt; loops keep running as long as a condition is true. They're perfect when you don't know in advance how many iterations you need.&lt;/p&gt;

&lt;h3&gt;
  
  
  Basic While Loop
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="nv"&gt;count&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nv"&gt;$count&lt;/span&gt; &lt;span class="nt"&gt;-le&lt;/span&gt; 5 &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Count: &lt;/span&gt;&lt;span class="nv"&gt;$count&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
  &lt;span class="o"&gt;((&lt;/span&gt;count++&lt;span class="o"&gt;))&lt;/span&gt;
&lt;span class="k"&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Reading a File Line by Line
&lt;/h3&gt;

&lt;p&gt;One of the most common uses of &lt;code&gt;while&lt;/code&gt; in real scripts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="nv"&gt;IFS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;read&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; line&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Line: &lt;/span&gt;&lt;span class="nv"&gt;$line&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;done&lt;/span&gt; &amp;lt; /etc/hosts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;IFS=&lt;/code&gt; prevents leading/trailing whitespace from being stripped. &lt;code&gt;-r&lt;/code&gt; prevents backslashes from being interpreted. Both are good habits.&lt;/p&gt;

&lt;h3&gt;
  
  
  Waiting for Something to Happen
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Waiting for server to come online..."&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt; curl &lt;span class="nt"&gt;-s&lt;/span&gt; http://localhost:8080 &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; /dev/null&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Not ready yet. Retrying in 3 seconds..."&lt;/span&gt;
  &lt;span class="nb"&gt;sleep &lt;/span&gt;3
&lt;span class="k"&gt;done

&lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Server is up!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This kind of loop is legitimately useful in deployment scripts where you need to wait for a service to start before proceeding.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; Always make sure your &lt;code&gt;while&lt;/code&gt; loop has a way to end. A missing increment or a condition that never becomes false will give you an infinite loop that you'll have to kill with &lt;code&gt;Ctrl+C&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Case Statements
&lt;/h2&gt;

&lt;p&gt;When you have one variable and multiple possible values to check against, &lt;code&gt;case&lt;/code&gt; is far cleaner than a chain of &lt;code&gt;if-elif&lt;/code&gt; statements.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Problem &lt;code&gt;case&lt;/code&gt; Solves
&lt;/h3&gt;

&lt;p&gt;Imagine checking what day of the week it is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Verbose and repetitive with if-elif&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$day&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Monday"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
  ...
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$day&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Tuesday"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
  ...
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$day&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Wednesday"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
  ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;case&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="nb"&gt;read&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; &lt;span class="s2"&gt;"Enter day: "&lt;/span&gt; day

&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nv"&gt;$day&lt;/span&gt; &lt;span class="k"&gt;in
  &lt;/span&gt;Monday&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Start of the week. Let's go."&lt;/span&gt; &lt;span class="p"&gt;;;&lt;/span&gt;
  Tuesday &lt;span class="p"&gt;|&lt;/span&gt; Wednesday &lt;span class="p"&gt;|&lt;/span&gt; Thursday&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Mid-week grind."&lt;/span&gt; &lt;span class="p"&gt;;;&lt;/span&gt;
  Friday&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Almost there."&lt;/span&gt; &lt;span class="p"&gt;;;&lt;/span&gt;
  Saturday &lt;span class="p"&gt;|&lt;/span&gt; Sunday&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Weekend. Close the laptop."&lt;/span&gt; &lt;span class="p"&gt;;;&lt;/span&gt;
  &lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"That's not a valid day."&lt;/span&gt; &lt;span class="p"&gt;;;&lt;/span&gt;
&lt;span class="k"&gt;esac&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each pattern ends with &lt;code&gt;)&lt;/code&gt;, the commands end with &lt;code&gt;;;&lt;/code&gt;, and &lt;code&gt;*)&lt;/code&gt; is the catch-all default (like &lt;code&gt;else&lt;/code&gt;). The block ends with &lt;code&gt;esac&lt;/code&gt; — that's &lt;code&gt;case&lt;/code&gt; backwards, a classic Unix move.&lt;/p&gt;

&lt;h3&gt;
  
  
  Practical Example: Script Menu
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"==============================="&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"     Server Management Menu"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"==============================="&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"1) Check disk usage"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"2) Check memory usage"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"3) List running processes"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"4) Exit"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;

&lt;span class="nb"&gt;read&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; &lt;span class="s2"&gt;"Choose an option: "&lt;/span&gt; option

&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nv"&gt;$option&lt;/span&gt; &lt;span class="k"&gt;in
  &lt;/span&gt;1&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;df&lt;/span&gt; &lt;span class="nt"&gt;-h&lt;/span&gt; &lt;span class="p"&gt;;;&lt;/span&gt;
  2&lt;span class="p"&gt;)&lt;/span&gt;
    free &lt;span class="nt"&gt;-h&lt;/span&gt; &lt;span class="p"&gt;;;&lt;/span&gt;
  3&lt;span class="p"&gt;)&lt;/span&gt;
    ps aux | &lt;span class="nb"&gt;head&lt;/span&gt; &lt;span class="nt"&gt;-20&lt;/span&gt; &lt;span class="p"&gt;;;&lt;/span&gt;
  4&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Goodbye."&lt;/span&gt;
    &lt;span class="nb"&gt;exit &lt;/span&gt;0 &lt;span class="p"&gt;;;&lt;/span&gt;
  &lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Invalid option."&lt;/span&gt; &lt;span class="p"&gt;;;&lt;/span&gt;
&lt;span class="k"&gt;esac&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This kind of menu-driven script is a pattern you'll see all over real DevOps tooling.&lt;/p&gt;




&lt;h2&gt;
  
  
  Exit Codes
&lt;/h2&gt;

&lt;p&gt;Every command you run in a terminal exits with a number. That number is the &lt;strong&gt;exit code&lt;/strong&gt;, and it tells you whether the command succeeded or failed.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;0&lt;/code&gt; = success&lt;/li&gt;
&lt;li&gt;Any non-zero value = failure (the specific number often indicates the type of error)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Checking &lt;code&gt;$?&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;$?&lt;/code&gt; holds the exit code of the last command that ran:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="nb"&gt;ls&lt;/span&gt; /tmp
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Exit code: &lt;/span&gt;&lt;span class="nv"&gt;$?&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;   &lt;span class="c"&gt;# 0 — /tmp exists&lt;/span&gt;

&lt;span class="nb"&gt;ls&lt;/span&gt; /nonexistent_directory
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Exit code: &lt;/span&gt;&lt;span class="nv"&gt;$?&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;   &lt;span class="c"&gt;# 2 — directory not found&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using Exit Codes in Conditionals
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

ping &lt;span class="nt"&gt;-c&lt;/span&gt; 1 google.com &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; /dev/null 2&amp;gt;&amp;amp;1

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nv"&gt;$?&lt;/span&gt; &lt;span class="nt"&gt;-eq&lt;/span&gt; 0 &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Internet is up."&lt;/span&gt;
&lt;span class="k"&gt;else
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"No internet connection."&lt;/span&gt;
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or more concisely — you can use the command directly in the &lt;code&gt;if&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="k"&gt;if &lt;/span&gt;ping &lt;span class="nt"&gt;-c&lt;/span&gt; 1 google.com &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; /dev/null 2&amp;gt;&amp;amp;1&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Internet is up."&lt;/span&gt;
&lt;span class="k"&gt;else
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"No internet connection."&lt;/span&gt;
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Setting Exit Codes in Your Scripts
&lt;/h3&gt;

&lt;p&gt;When writing scripts that other scripts or tools will call, always set meaningful exit codes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="nv"&gt;backup_file&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"/backups/db.sql"&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$backup_file&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"ERROR: Backup file not found."&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&amp;amp;2
  &lt;span class="nb"&gt;exit &lt;/span&gt;1
&lt;span class="k"&gt;fi

&lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Backup file found. Proceeding..."&lt;/span&gt;
&lt;span class="nb"&gt;exit &lt;/span&gt;0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;&amp;gt;&amp;amp;2&lt;/code&gt; redirects error messages to stderr instead of stdout — the right place for errors. &lt;code&gt;exit 1&lt;/code&gt; signals failure to whatever called your script.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; In CI/CD pipelines, exit codes are everything. A script that exits with &lt;code&gt;0&lt;/code&gt; tells the pipeline "all good, move on." A non-zero exit code stops the pipeline and flags the step as failed. Always think about your exit codes when writing automation scripts.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Functions in Bash
&lt;/h2&gt;

&lt;p&gt;Once your scripts get longer than 30-40 lines, repeating the same blocks of code starts to hurt. Functions let you name a block of code and call it whenever you need it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Defining and Calling a Function
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

greet&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Hello, &lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="s2"&gt;!"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

greet &lt;span class="s2"&gt;"Malhar"&lt;/span&gt;
greet &lt;span class="s2"&gt;"World"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello, Malhar!
Hello, World!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function is defined first, then called by name. Arguments passed to the function are available as &lt;code&gt;$1&lt;/code&gt;, &lt;code&gt;$2&lt;/code&gt;, etc. — just like script arguments, but scoped to the function.&lt;/p&gt;

&lt;h3&gt;
  
  
  A More Realistic Function
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

log&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;local &lt;/span&gt;&lt;span class="nv"&gt;level&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;
  &lt;span class="nb"&gt;local &lt;/span&gt;&lt;span class="nv"&gt;message&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$2&lt;/span&gt;
  &lt;span class="nb"&gt;local &lt;/span&gt;&lt;span class="nv"&gt;timestamp&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt; &lt;span class="s2"&gt;"+%Y-%m-%d %H:%M:%S"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"[&lt;/span&gt;&lt;span class="nv"&gt;$timestamp&lt;/span&gt;&lt;span class="s2"&gt;] [&lt;/span&gt;&lt;span class="nv"&gt;$level&lt;/span&gt;&lt;span class="s2"&gt;] &lt;/span&gt;&lt;span class="nv"&gt;$message&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

log &lt;span class="s2"&gt;"INFO"&lt;/span&gt;  &lt;span class="s2"&gt;"Script started."&lt;/span&gt;
log &lt;span class="s2"&gt;"INFO"&lt;/span&gt;  &lt;span class="s2"&gt;"Checking disk usage..."&lt;/span&gt;
log &lt;span class="s2"&gt;"WARN"&lt;/span&gt;  &lt;span class="s2"&gt;"Disk usage above 80%."&lt;/span&gt;
log &lt;span class="s2"&gt;"ERROR"&lt;/span&gt; &lt;span class="s2"&gt;"Backup directory not found."&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;[2025-03-10 10:42:01] [INFO] Script started.
[2025-03-10 10:42:01] [INFO] Checking disk usage...
[2025-03-10 10:42:01] [WARN] Disk usage above 80%.
[2025-03-10 10:42:01] [ERROR] Backup directory not found.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This &lt;code&gt;log&lt;/code&gt; function is something you'd actually copy into real scripts.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;local&lt;/code&gt; Variables
&lt;/h3&gt;

&lt;p&gt;Notice the &lt;code&gt;local&lt;/code&gt; keyword. Variables inside functions are global by default in Bash, which can cause bugs in longer scripts. Declaring them with &lt;code&gt;local&lt;/code&gt; keeps them scoped to the function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="nv"&gt;count&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;10   &lt;span class="c"&gt;# global&lt;/span&gt;

increment&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;local &lt;/span&gt;&lt;span class="nv"&gt;count&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0   &lt;span class="c"&gt;# local — doesn't touch the global $count&lt;/span&gt;
  &lt;span class="o"&gt;((&lt;/span&gt;count++&lt;span class="o"&gt;))&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Inside function: &lt;/span&gt;&lt;span class="nv"&gt;$count&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

increment
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Outside function: &lt;/span&gt;&lt;span class="nv"&gt;$count&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Inside function: 1
Outside function: 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Functions with Return Values
&lt;/h3&gt;

&lt;p&gt;Bash functions don't return values the way other languages do. They return exit codes (0-255). To "return" a value, you either &lt;code&gt;echo&lt;/code&gt; it and capture it with &lt;code&gt;$()&lt;/code&gt;, or use a global variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

get_disk_usage&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;local &lt;/span&gt;&lt;span class="nv"&gt;path&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;1&lt;/span&gt;&lt;span class="k"&gt;:-&lt;/span&gt;&lt;span class="s2"&gt;"/"&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;
  &lt;span class="nb"&gt;df&lt;/span&gt; &lt;span class="nt"&gt;-h&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$path&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'NR==2 {print $5}'&lt;/span&gt; | &lt;span class="nb"&gt;tr&lt;/span&gt; &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s1"&gt;'%'&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;usage&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;get_disk_usage &lt;span class="s2"&gt;"/"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Disk usage on /: &lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;usage&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;%"&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$usage&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;-gt&lt;/span&gt; 80 &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Warning: disk usage is high."&lt;/span&gt;
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Real Automation Script: System Health Check
&lt;/h2&gt;

&lt;p&gt;Let's put everything together — conditionals, loops, functions, exit codes — into something you'd actually run on a server.&lt;/p&gt;

&lt;p&gt;This script performs a basic system health check and prints a summary report.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="c"&gt;# ─────────────────────────────────────────────────────&lt;/span&gt;
&lt;span class="c"&gt;# system_health_check.sh&lt;/span&gt;
&lt;span class="c"&gt;# Checks disk, memory, CPU load, and running services&lt;/span&gt;
&lt;span class="c"&gt;# Usage: ./system_health_check.sh&lt;/span&gt;
&lt;span class="c"&gt;# ─────────────────────────────────────────────────────&lt;/span&gt;

&lt;span class="c"&gt;# ── Thresholds ────────────────────────────────────────&lt;/span&gt;
&lt;span class="nv"&gt;DISK_THRESHOLD&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;80
&lt;span class="nv"&gt;MEM_THRESHOLD&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;85
&lt;span class="nv"&gt;SERVICES&lt;/span&gt;&lt;span class="o"&gt;=(&lt;/span&gt;&lt;span class="s2"&gt;"nginx"&lt;/span&gt; &lt;span class="s2"&gt;"ssh"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="c"&gt;# ── Colors (optional but makes output readable) ───────&lt;/span&gt;
&lt;span class="nv"&gt;RED&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'\033[0;31m'&lt;/span&gt;
&lt;span class="nv"&gt;GREEN&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'\033[0;32m'&lt;/span&gt;
&lt;span class="nv"&gt;YELLOW&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'\033[1;33m'&lt;/span&gt;
&lt;span class="nv"&gt;NC&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'\033[0m'&lt;/span&gt;   &lt;span class="c"&gt;# No Color (reset)&lt;/span&gt;

&lt;span class="c"&gt;# ── Logging Function ──────────────────────────────────&lt;/span&gt;
log&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;local &lt;/span&gt;&lt;span class="nv"&gt;level&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;
  &lt;span class="nb"&gt;local &lt;/span&gt;&lt;span class="nv"&gt;message&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$2&lt;/span&gt;
  &lt;span class="nb"&gt;local &lt;/span&gt;&lt;span class="nv"&gt;timestamp&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt; &lt;span class="s2"&gt;"+%Y-%m-%d %H:%M:%S"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;

  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nv"&gt;$level&lt;/span&gt; &lt;span class="k"&gt;in
    &lt;/span&gt;INFO&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;GREEN&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;$timestamp&lt;/span&gt;&lt;span class="s2"&gt;] [INFO]&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;NC&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;  &lt;/span&gt;&lt;span class="nv"&gt;$message&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="p"&gt;;;&lt;/span&gt;
    WARN&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;YELLOW&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;$timestamp&lt;/span&gt;&lt;span class="s2"&gt;] [WARN]&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;NC&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;  &lt;/span&gt;&lt;span class="nv"&gt;$message&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="p"&gt;;;&lt;/span&gt;
    ERROR&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;RED&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;$timestamp&lt;/span&gt;&lt;span class="s2"&gt;] [ERROR]&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;NC&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="nv"&gt;$message&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="p"&gt;;;&lt;/span&gt;
  &lt;span class="k"&gt;esac&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;# ── Check Disk Usage ──────────────────────────────────&lt;/span&gt;
check_disk&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  log &lt;span class="s2"&gt;"INFO"&lt;/span&gt; &lt;span class="s2"&gt;"Checking disk usage..."&lt;/span&gt;

  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="nv"&gt;IFS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;read&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; line&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do
    &lt;/span&gt;&lt;span class="nv"&gt;usage&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$line&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{print $5}'&lt;/span&gt; | &lt;span class="nb"&gt;tr&lt;/span&gt; &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s1"&gt;'%'&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;
    &lt;span class="nv"&gt;mount&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$line&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{print $6}'&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$usage&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;-ge&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$DISK_THRESHOLD&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
      &lt;/span&gt;log &lt;span class="s2"&gt;"WARN"&lt;/span&gt; &lt;span class="s2"&gt;"Disk usage on &lt;/span&gt;&lt;span class="nv"&gt;$mount&lt;/span&gt;&lt;span class="s2"&gt; is at &lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;usage&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;% — above threshold of &lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;DISK_THRESHOLD&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;%"&lt;/span&gt;
    &lt;span class="k"&gt;else
      &lt;/span&gt;log &lt;span class="s2"&gt;"INFO"&lt;/span&gt; &lt;span class="s2"&gt;"Disk usage on &lt;/span&gt;&lt;span class="nv"&gt;$mount&lt;/span&gt;&lt;span class="s2"&gt;: &lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;usage&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;% — OK"&lt;/span&gt;
    &lt;span class="k"&gt;fi
  done&lt;/span&gt; &amp;lt; &amp;lt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;df&lt;/span&gt; &lt;span class="nt"&gt;-h&lt;/span&gt; | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'NR&amp;gt;1 &amp;amp;&amp;amp; $1 ~ /^\/dev/'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;# ── Check Memory Usage ────────────────────────────────&lt;/span&gt;
check_memory&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  log &lt;span class="s2"&gt;"INFO"&lt;/span&gt; &lt;span class="s2"&gt;"Checking memory usage..."&lt;/span&gt;

  &lt;span class="nb"&gt;local &lt;/span&gt;&lt;span class="nv"&gt;total&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;free | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'/^Mem:/ {print $2}'&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;
  &lt;span class="nb"&gt;local &lt;/span&gt;&lt;span class="nv"&gt;used&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;free | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'/^Mem:/ {print $3}'&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;
  &lt;span class="nb"&gt;local &lt;/span&gt;&lt;span class="nv"&gt;usage&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;$((&lt;/span&gt; used &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="m"&gt;100&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; total &lt;span class="k"&gt;))&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$usage&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;-ge&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$MEM_THRESHOLD&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
    &lt;/span&gt;log &lt;span class="s2"&gt;"WARN"&lt;/span&gt; &lt;span class="s2"&gt;"Memory usage is at &lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;usage&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;% — above threshold of &lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;MEM_THRESHOLD&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;%"&lt;/span&gt;
  &lt;span class="k"&gt;else
    &lt;/span&gt;log &lt;span class="s2"&gt;"INFO"&lt;/span&gt; &lt;span class="s2"&gt;"Memory usage: &lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;usage&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;% — OK"&lt;/span&gt;
  &lt;span class="k"&gt;fi&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;# ── Check CPU Load ────────────────────────────────────&lt;/span&gt;
check_cpu&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  log &lt;span class="s2"&gt;"INFO"&lt;/span&gt; &lt;span class="s2"&gt;"Checking CPU load..."&lt;/span&gt;

  &lt;span class="nb"&gt;local &lt;/span&gt;&lt;span class="nv"&gt;load&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;uptime&lt;/span&gt; | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="nt"&gt;-F&lt;/span&gt;&lt;span class="s1"&gt;'load average:'&lt;/span&gt; &lt;span class="s1"&gt;'{print $2}'&lt;/span&gt; | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="nt"&gt;-F&lt;/span&gt;&lt;span class="s1"&gt;','&lt;/span&gt; &lt;span class="s1"&gt;'{print $1}'&lt;/span&gt; | xargs&lt;span class="si"&gt;)&lt;/span&gt;
  log &lt;span class="s2"&gt;"INFO"&lt;/span&gt; &lt;span class="s2"&gt;"1-minute load average: &lt;/span&gt;&lt;span class="nv"&gt;$load&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;# ── Check Services ────────────────────────────────────&lt;/span&gt;
check_services&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  log &lt;span class="s2"&gt;"INFO"&lt;/span&gt; &lt;span class="s2"&gt;"Checking critical services..."&lt;/span&gt;

  &lt;span class="k"&gt;for &lt;/span&gt;service &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;SERVICES&lt;/span&gt;&lt;span class="p"&gt;[@]&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do
    if &lt;/span&gt;systemctl is-active &lt;span class="nt"&gt;--quiet&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$service&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; 2&amp;gt;/dev/null&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
      &lt;/span&gt;log &lt;span class="s2"&gt;"INFO"&lt;/span&gt; &lt;span class="s2"&gt;"Service [&lt;/span&gt;&lt;span class="nv"&gt;$service&lt;/span&gt;&lt;span class="s2"&gt;] is running — OK"&lt;/span&gt;
    &lt;span class="k"&gt;else
      &lt;/span&gt;log &lt;span class="s2"&gt;"ERROR"&lt;/span&gt; &lt;span class="s2"&gt;"Service [&lt;/span&gt;&lt;span class="nv"&gt;$service&lt;/span&gt;&lt;span class="s2"&gt;] is NOT running!"&lt;/span&gt;
    &lt;span class="k"&gt;fi
  done&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;# ── Main ──────────────────────────────────────────────&lt;/span&gt;
main&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"=============================================="&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"       System Health Check"&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"       &lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"       Host: &lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;hostname&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"=============================================="&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;

  check_disk
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;
  check_memory
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;
  check_cpu
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;
  check_services

  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"=============================================="&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"  Health check complete."&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"=============================================="&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

main
&lt;span class="nb"&gt;exit &lt;/span&gt;0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make it executable and run it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;chmod&lt;/span&gt; +x system_health_check.sh
./system_health_check.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sample output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;==============================================
       System Health Check
       Mon Mar 10 10:45:01 IST 2025
       Host: dev-server
==============================================

[2025-03-10 10:45:01] [INFO]  Checking disk usage...
[2025-03-10 10:45:01] [INFO]  Disk usage on /: 43% — OK
[2025-03-10 10:45:01] [WARN]  Disk usage on /data is at 87% — above threshold of 80%

[2025-03-10 10:45:01] [INFO]  Checking memory usage...
[2025-03-10 10:45:01] [INFO]  Memory usage: 61% — OK

[2025-03-10 10:45:01] [INFO]  Checking CPU load...
[2025-03-10 10:45:01] [INFO]  1-minute load average: 0.42

[2025-03-10 10:45:01] [INFO]  Checking critical services...
[2025-03-10 10:45:01] [INFO]  Service [nginx] is running — OK
[2025-03-10 10:45:01] [ERROR] Service [ssh] is NOT running!

==============================================
  Health check complete.
==============================================
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the kind of script that runs daily via cron on real servers. Add an email or Slack notification at the end and you have a basic monitoring system.&lt;/p&gt;




&lt;h2&gt;
  
  
  Best Practices for Shell Scripts
&lt;/h2&gt;

&lt;p&gt;Writing a script that works is one thing. Writing a script that someone else (or future you) can read, debug, and maintain is another.&lt;/p&gt;

&lt;h3&gt;
  
  
  Always Quote Your Variables
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Bad — breaks if filename has spaces&lt;/span&gt;
&lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="nv"&gt;$filename&lt;/span&gt;

&lt;span class="c"&gt;# Good&lt;/span&gt;
&lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$filename&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the single most common source of bugs in shell scripts. Filenames, paths, and user input can all contain spaces. Always quote.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use Meaningful Names
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Hard to read&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;f &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt;.log&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do
  &lt;/span&gt;&lt;span class="nv"&gt;s&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;wc&lt;/span&gt; &lt;span class="nt"&gt;-l&lt;/span&gt; &amp;lt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$f&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$f&lt;/span&gt;&lt;span class="s2"&gt;: &lt;/span&gt;&lt;span class="nv"&gt;$s&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;done&lt;/span&gt;

&lt;span class="c"&gt;# Clear&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;log_file &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt;.log&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do
  &lt;/span&gt;&lt;span class="nv"&gt;line_count&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;wc&lt;/span&gt; &lt;span class="nt"&gt;-l&lt;/span&gt; &amp;lt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$log_file&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$log_file&lt;/span&gt;&lt;span class="s2"&gt;: &lt;/span&gt;&lt;span class="nv"&gt;$line_count&lt;/span&gt;&lt;span class="s2"&gt; lines"&lt;/span&gt;
&lt;span class="k"&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Use &lt;code&gt;set -e&lt;/code&gt; and &lt;code&gt;set -u&lt;/code&gt; at the Top
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt;   &lt;span class="c"&gt;# Exit immediately if any command fails&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt; &lt;span class="nt"&gt;-u&lt;/span&gt;   &lt;span class="c"&gt;# Treat unset variables as errors&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;set -e&lt;/code&gt; means your script won't silently continue after a failed command. &lt;code&gt;set -u&lt;/code&gt; catches typos in variable names. Add these to every script you write seriously.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use ShellCheck
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.shellcheck.net" rel="noopener noreferrer"&gt;ShellCheck&lt;/a&gt; is a free static analysis tool for shell scripts. Paste your script in and it'll catch bugs, bad practices, and portability issues before you even run the script.&lt;/p&gt;

&lt;p&gt;You can also install it locally:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Ubuntu/Debian&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;shellcheck

&lt;span class="c"&gt;# Then run it on your script&lt;/span&gt;
shellcheck your_script.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's the bash equivalent of a linter. Use it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Add a Usage Comment at the Top
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
&lt;span class="c"&gt;# ─────────────────────────────────────────&lt;/span&gt;
&lt;span class="c"&gt;# backup.sh&lt;/span&gt;
&lt;span class="c"&gt;# Description: Backs up a directory to a target location&lt;/span&gt;
&lt;span class="c"&gt;# Usage: ./backup.sh &amp;lt;source_dir&amp;gt; &amp;lt;target_dir&amp;gt;&lt;/span&gt;
&lt;span class="c"&gt;# Author: Malhar Gupte&lt;/span&gt;
&lt;span class="c"&gt;# Last updated: 2025-03-10&lt;/span&gt;
&lt;span class="c"&gt;# ─────────────────────────────────────────&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Takes 30 seconds and saves a lot of confusion when you revisit the script months later.&lt;/p&gt;

&lt;h3&gt;
  
  
  Redirect Error Messages to stderr
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Good practice for error messages&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"ERROR: File not found."&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&amp;amp;2
&lt;span class="nb"&gt;exit &lt;/span&gt;1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means error messages won't pollute the stdout output of your script, which matters when other scripts or pipelines consume your script's output.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;If you've made it through both parts of this series, you can now write scripts that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Make decisions with &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;elif&lt;/code&gt;, &lt;code&gt;case&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Repeat tasks with &lt;code&gt;for&lt;/code&gt; and &lt;code&gt;while&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Handle success and failure with exit codes&lt;/li&gt;
&lt;li&gt;Stay organized with functions&lt;/li&gt;
&lt;li&gt;Check real system health on a live server&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's not beginner knowledge anymore.&lt;/p&gt;

&lt;p&gt;For DevOps, shell scripting is the connective tissue between tools. It's how you glue together Docker commands, AWS CLI calls, database backups, log rotations, and deployment steps into a single automated workflow. Even if you work primarily with Python or Go for automation eventually, knowing how to read and write Bash means you can work on any server, in any environment, without needing anything installed.&lt;/p&gt;

&lt;p&gt;The best way to get better at this is to find something you do manually and automate it. Clean up old files? Script it. SSH into a server and run the same three commands? Script it. Check if your services are running? You just wrote that script.&lt;/p&gt;

&lt;p&gt;Start small. The scripts you write for yourself are the ones you'll actually learn from.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;That's a wrap on this series. If something wasn't clear or you ran into an issue, drop a comment — happy to help debug. And if you're building something interesting with what you learned, share it.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Part 1 is here if you missed it: &lt;a href="https://dev.to/guptem/shell-scripting-for-beginners-from-zero-to-automating-your-first-tasks-part-1-2of0"&gt;Shell Scripting for Beginners — Part 1&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>automation</category>
      <category>beginners</category>
      <category>linux</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Shell Scripting for Beginners: From Zero to Automating Your First Tasks (Part 1)</title>
      <dc:creator>Malhar Gupte</dc:creator>
      <pubDate>Tue, 10 Mar 2026 05:22:21 +0000</pubDate>
      <link>https://dev.to/guptem/shell-scripting-for-beginners-from-zero-to-automating-your-first-tasks-part-1-2of0</link>
      <guid>https://dev.to/guptem/shell-scripting-for-beginners-from-zero-to-automating-your-first-tasks-part-1-2of0</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Series:&lt;/strong&gt; Shell Scripting for Beginners | &lt;strong&gt;Part:&lt;/strong&gt; 1 of 2&lt;br&gt;
&lt;strong&gt;Level:&lt;/strong&gt; Beginner | &lt;strong&gt;Time to read:&lt;/strong&gt; ~15 minutes&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;There's a moment every developer eventually hits — you find yourself doing the same repetitive task for the tenth time that week. Maybe it's renaming a batch of files, SSHing into servers and running the same commands, or cleaning up log directories. You do it manually, again, and somewhere in the back of your head a little voice says: &lt;em&gt;"There has to be a better way."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;That better way is shell scripting.&lt;/p&gt;

&lt;p&gt;Shell scripting is one of those foundational skills that quietly separates developers who feel comfortable on a Linux system from those who feel like they're just guessing. It's not glamorous. It won't land you on the front page of Hacker News. But the ability to write a script that saves you 20 minutes every day? Over a year, that's more than 120 hours back in your pocket.&lt;/p&gt;

&lt;p&gt;For DevOps engineers and sysadmins, scripting isn't optional — it's oxygen. Deployments, backups, health checks, log rotation, user provisioning — all of it gets automated with shell scripts. But even if you're a backend developer or just getting started with Linux, learning to write basic scripts will make your day-to-day dramatically more efficient.&lt;/p&gt;

&lt;p&gt;This guide is Part 1 of a two-part series. We're going to go from &lt;em&gt;"what even is a shell?"&lt;/em&gt; to writing a working script that takes arguments, reads user input, and does real arithmetic — all from scratch. No experience needed.&lt;/p&gt;

&lt;p&gt;Let's get into it.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is a Shell Script?
&lt;/h2&gt;

&lt;p&gt;Before we write anything, let's get the vocabulary straight.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Shell
&lt;/h3&gt;

&lt;p&gt;When you open a terminal on Linux or macOS, you're not talking directly to the operating system kernel. There's a program sitting in between — that's the &lt;strong&gt;shell&lt;/strong&gt;. It reads the commands you type, interprets them, and tells the OS what to do.&lt;/p&gt;

&lt;p&gt;Think of it like a translator. You speak "human command language," the shell translates it, and the OS executes it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bash
&lt;/h3&gt;

&lt;p&gt;There are several shell programs out there — &lt;code&gt;sh&lt;/code&gt;, &lt;code&gt;zsh&lt;/code&gt;, &lt;code&gt;fish&lt;/code&gt;, &lt;code&gt;dash&lt;/code&gt; — but the one you'll encounter most in the wild, especially on Linux servers, is &lt;strong&gt;Bash&lt;/strong&gt; (Bourne Again SHell). It's been around since 1989, it's installed on virtually every Linux system, and it's what most shell scripting tutorials (including this one) use.&lt;/p&gt;

&lt;p&gt;When you open a terminal and start typing commands, there's a very good chance you're already using Bash.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Check which shell you're currently using&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$SHELL&lt;/span&gt;

&lt;span class="c"&gt;# Check your bash version&lt;/span&gt;
bash &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What is a Shell Script?
&lt;/h3&gt;

&lt;p&gt;A shell script is just a plain text file that contains a series of commands — the same commands you'd type in a terminal, but saved in a file so they can be run all at once, on demand, automatically.&lt;/p&gt;

&lt;p&gt;That's it. There's no compiling, no special runtime. It's text that the shell reads and executes line by line.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why is Scripting So Powerful?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Repeatability&lt;/strong&gt; — Run the exact same sequence of commands every time, with no typos or forgotten steps.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Automation&lt;/strong&gt; — Schedule scripts to run at 3am without you being there.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Speed&lt;/strong&gt; — What takes you 10 manual commands takes a script half a second.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Portability&lt;/strong&gt; — A script that works on one Linux server will usually work on another.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Creating Your First Script
&lt;/h2&gt;

&lt;p&gt;Time to write something. Open your terminal and follow along.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Create the Script File
&lt;/h3&gt;

&lt;p&gt;A shell script is just a file. By convention, shell script files end with &lt;code&gt;.sh&lt;/code&gt;, though technically the extension doesn't matter to the shell.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Create a new file called hello.sh&lt;/span&gt;
&lt;span class="nb"&gt;touch &lt;/span&gt;hello.sh

&lt;span class="c"&gt;# Open it in a text editor&lt;/span&gt;
nano hello.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Hello, World!"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"My first shell script is running."&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save and exit (&lt;code&gt;Ctrl+O&lt;/code&gt;, then &lt;code&gt;Ctrl+X&lt;/code&gt; in nano).&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Make It Executable
&lt;/h3&gt;

&lt;p&gt;By default, a new file doesn't have execute permission. If you try to run it as-is, you'll get a "Permission denied" error. You need to tell Linux that this file is allowed to be executed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;chmod&lt;/span&gt; +x hello.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;chmod&lt;/code&gt; stands for &lt;em&gt;change mode&lt;/em&gt;. The &lt;code&gt;+x&lt;/code&gt; adds execute permission for the file owner. You only have to do this once per script.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; Run &lt;code&gt;ls -l hello.sh&lt;/code&gt; before and after &lt;code&gt;chmod +x&lt;/code&gt; to see the permissions change. You'll notice an &lt;code&gt;x&lt;/code&gt; appear in the permission string.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Step 3: Run the Script
&lt;/h3&gt;

&lt;p&gt;Now you can execute it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./hello.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello, World!
My first shell script is running.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;./&lt;/code&gt; prefix tells the shell to look for the file in the current directory. Without it, the shell would only look in directories listed in your &lt;code&gt;$PATH&lt;/code&gt; environment variable.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Alternative:&lt;/strong&gt; You can also run it as &lt;code&gt;bash hello.sh&lt;/code&gt; — this works even without &lt;code&gt;chmod +x&lt;/code&gt;. But using &lt;code&gt;./&lt;/code&gt; with execute permissions is the standard practice.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Understanding the Shebang
&lt;/h2&gt;

&lt;p&gt;You probably noticed the first line of the script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is called the &lt;strong&gt;shebang&lt;/strong&gt; (also written as "hashbang"). It's not a comment, even though it starts with &lt;code&gt;#&lt;/code&gt;. It's a special directive for the operating system.&lt;/p&gt;

&lt;h3&gt;
  
  
  How It Works
&lt;/h3&gt;

&lt;p&gt;When you execute a file, the OS looks at the very first two characters. If it sees &lt;code&gt;#!&lt;/code&gt;, it knows: &lt;em&gt;"This is a script — the path that follows tells me which interpreter to use."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So &lt;code&gt;#!/bin/bash&lt;/code&gt; is saying: &lt;em&gt;"Use the program located at &lt;code&gt;/bin/bash&lt;/code&gt; to execute this script."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you were writing a Python script instead, you might use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/usr/bin/env python3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or for a Node.js script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/usr/bin/env node&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Always Include It
&lt;/h3&gt;

&lt;p&gt;Some scripts will run without a shebang, but the behavior depends on whatever shell happens to be running them — which can vary between systems. Always include &lt;code&gt;#!/bin/bash&lt;/code&gt; at the top of your Bash scripts. It makes your script's behavior explicit and predictable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="c"&gt;# Your script starts here&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Shebang ensures I always run with Bash"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; Use &lt;code&gt;which bash&lt;/code&gt; to find the exact path to Bash on your system. On most systems it's &lt;code&gt;/bin/bash&lt;/code&gt;, but on some (like certain macOS setups with Homebrew) it might be &lt;code&gt;/usr/local/bin/bash&lt;/code&gt;. Using &lt;code&gt;#!/usr/bin/env bash&lt;/code&gt; is a more portable alternative that searches your &lt;code&gt;$PATH&lt;/code&gt; for bash.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Variables in Shell Scripts
&lt;/h2&gt;

&lt;p&gt;Variables let you store values and reuse them. In Bash, they're simple to declare — maybe a little too simple, because the syntax is strict and easy to get wrong at first.&lt;/p&gt;

&lt;h3&gt;
  
  
  Declaring a Variable
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Malhar"&lt;/span&gt;
&lt;span class="nv"&gt;age&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;21
&lt;span class="nv"&gt;city&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Vadodara"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Rules to live by:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No spaces around the &lt;code&gt;=&lt;/code&gt; sign. &lt;code&gt;name = "Malhar"&lt;/code&gt; will fail. It must be &lt;code&gt;name="Malhar"&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Variable names are case-sensitive. &lt;code&gt;Name&lt;/code&gt; and &lt;code&gt;name&lt;/code&gt; are different variables.&lt;/li&gt;
&lt;li&gt;By convention, use lowercase for regular script variables and UPPERCASE for environment variables and constants.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Using a Variable
&lt;/h3&gt;

&lt;p&gt;To use (or "reference") a variable, prefix its name with &lt;code&gt;$&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Malhar"&lt;/span&gt;
&lt;span class="nv"&gt;city&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Vadodara"&lt;/span&gt;

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Hello, my name is &lt;/span&gt;&lt;span class="nv"&gt;$name&lt;/span&gt;&lt;span class="s2"&gt; and I live in &lt;/span&gt;&lt;span class="nv"&gt;$city&lt;/span&gt;&lt;span class="s2"&gt;."&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello, my name is Malhar and I live in Vadodara.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Curly Brace Syntax
&lt;/h3&gt;

&lt;p&gt;When a variable name could be ambiguous — like when you're placing it right next to other text — wrap it in curly braces:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="nv"&gt;animal&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"dog"&lt;/span&gt;

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"I have one &lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;animal&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;."&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"I have two &lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;animal&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;s."&lt;/span&gt;   &lt;span class="c"&gt;# Without braces, $animals would fail&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;I have one dog.
I have two dogs.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; Get in the habit of using &lt;code&gt;${variable}&lt;/code&gt; over &lt;code&gt;$variable&lt;/code&gt; — it prevents subtle bugs and makes your scripts easier to read.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Storing Command Output in a Variable
&lt;/h3&gt;

&lt;p&gt;This is a technique you'll use constantly — capturing the output of a command into a variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="nv"&gt;current_date&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;current_user&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;whoami&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Script run by: &lt;/span&gt;&lt;span class="nv"&gt;$current_user&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Date: &lt;/span&gt;&lt;span class="nv"&gt;$current_date&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;$(...)&lt;/code&gt; syntax runs the command inside and captures its output. This is called &lt;strong&gt;command substitution&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Command Line Arguments
&lt;/h2&gt;

&lt;p&gt;Scripts become far more useful when they can accept input at the time you run them — without needing to edit the file.&lt;/p&gt;

&lt;h3&gt;
  
  
  How Arguments Work
&lt;/h3&gt;

&lt;p&gt;When you run a script like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./greet.sh Alice DevOps
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Bash automatically populates special variables:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Variable&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;$0&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;./greet.sh&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The name of the script itself&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;$1&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Alice&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;First argument&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;$2&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;DevOps&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Second argument&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;$3&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;(empty)&lt;/td&gt;
&lt;td&gt;Third argument (not provided here)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;$#&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;2&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Total number of arguments passed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;$@&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Alice DevOps&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;All arguments as a list&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Example Script
&lt;/h3&gt;

&lt;p&gt;Create a file called &lt;code&gt;greet.sh&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Script name: &lt;/span&gt;&lt;span class="nv"&gt;$0&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Hello, &lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="s2"&gt;! You work in &lt;/span&gt;&lt;span class="nv"&gt;$2&lt;/span&gt;&lt;span class="s2"&gt;."&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Total arguments passed: $#"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make it executable and run it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;chmod&lt;/span&gt; +x greet.sh
./greet.sh Alice DevOps
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Script name: ./greet.sh
Hello, Alice! You work in DevOps.
Total arguments passed: 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What if an Argument Isn't Provided?
&lt;/h3&gt;

&lt;p&gt;If &lt;code&gt;$1&lt;/code&gt; is empty because no argument was given, your script might behave unexpectedly. A good practice (which we'll cover more in Part 2 with conditionals) is to provide a default:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;1&lt;/span&gt;&lt;span class="k"&gt;:-&lt;/span&gt;&lt;span class="s2"&gt;"stranger"&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;   &lt;span class="c"&gt;# Use $1 if provided, otherwise default to "stranger"&lt;/span&gt;

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Hello, &lt;/span&gt;&lt;span class="nv"&gt;$name&lt;/span&gt;&lt;span class="s2"&gt;!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./greet.sh           &lt;span class="c"&gt;# → Hello, stranger!&lt;/span&gt;
./greet.sh Malhar    &lt;span class="c"&gt;# → Hello, Malhar!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; &lt;code&gt;${variable:-default}&lt;/code&gt; is a Bash parameter expansion trick. The &lt;code&gt;:-&lt;/code&gt; means "use this default value if the variable is unset or empty." Keep this one handy.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Taking Input from the User
&lt;/h2&gt;

&lt;p&gt;Sometimes you want the script to pause and ask the user something while it's running. That's what the &lt;code&gt;read&lt;/code&gt; command is for.&lt;/p&gt;

&lt;h3&gt;
  
  
  Basic &lt;code&gt;read&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"What is your name?"&lt;/span&gt;
&lt;span class="nb"&gt;read &lt;/span&gt;user_name
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Nice to meet you, &lt;/span&gt;&lt;span class="nv"&gt;$user_name&lt;/span&gt;&lt;span class="s2"&gt;!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The script will wait after printing the question. When the user types something and hits Enter, whatever they typed gets stored in &lt;code&gt;user_name&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;read -p&lt;/code&gt; (Prompt on the Same Line)
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;-p&lt;/code&gt; flag lets you combine the prompt and the input on the same line, which looks cleaner:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="nb"&gt;read&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; &lt;span class="s2"&gt;"Enter your name: "&lt;/span&gt; user_name
&lt;span class="nb"&gt;read&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; &lt;span class="s2"&gt;"Enter your city: "&lt;/span&gt; user_city

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Welcome, &lt;/span&gt;&lt;span class="nv"&gt;$user_name&lt;/span&gt;&lt;span class="s2"&gt; from &lt;/span&gt;&lt;span class="nv"&gt;$user_city&lt;/span&gt;&lt;span class="s2"&gt;!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output (with user input in italics):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Enter your name: Malhar
Enter your city: Vadodara

Welcome, Malhar from Vadodara!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;read -s&lt;/code&gt; (Silent Input)
&lt;/h3&gt;

&lt;p&gt;For sensitive input like passwords, use &lt;code&gt;-s&lt;/code&gt; to hide what the user types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="nb"&gt;read&lt;/span&gt; &lt;span class="nt"&gt;-sp&lt;/span&gt; &lt;span class="s2"&gt;"Enter your password: "&lt;/span&gt; user_pass
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Password received (not printing it, obviously)."&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Arithmetic Operations in Bash
&lt;/h2&gt;

&lt;p&gt;Bash handles arithmetic a bit differently than most programming languages. The most reliable and readable way to do arithmetic is with the &lt;code&gt;$(( ))&lt;/code&gt; syntax.&lt;/p&gt;

&lt;h3&gt;
  
  
  Basic Operations
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="nv"&gt;a&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;10
&lt;span class="nv"&gt;b&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;3

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Addition:       &lt;/span&gt;&lt;span class="k"&gt;$((&lt;/span&gt;a &lt;span class="o"&gt;+&lt;/span&gt; b&lt;span class="k"&gt;))&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Subtraction:    &lt;/span&gt;&lt;span class="k"&gt;$((&lt;/span&gt;a &lt;span class="o"&gt;-&lt;/span&gt; b&lt;span class="k"&gt;))&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Multiplication: &lt;/span&gt;&lt;span class="k"&gt;$((&lt;/span&gt;a &lt;span class="o"&gt;*&lt;/span&gt; b&lt;span class="k"&gt;))&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Division:       &lt;/span&gt;&lt;span class="k"&gt;$((&lt;/span&gt;a &lt;span class="o"&gt;/&lt;/span&gt; b&lt;span class="k"&gt;))&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;   &lt;span class="c"&gt;# Integer division only&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Modulo:         &lt;/span&gt;&lt;span class="k"&gt;$((&lt;/span&gt;a &lt;span class="o"&gt;%&lt;/span&gt; b&lt;span class="k"&gt;))&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Exponentiation: &lt;/span&gt;&lt;span class="k"&gt;$((&lt;/span&gt;a &lt;span class="o"&gt;**&lt;/span&gt; b&lt;span class="k"&gt;))&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Addition:       13
Subtraction:    7
Multiplication: 30
Division:       3
Modulo:         1
Exponentiation: 1000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; Bash only does &lt;strong&gt;integer arithmetic&lt;/strong&gt; by default. &lt;code&gt;$((10 / 3))&lt;/code&gt; gives you &lt;code&gt;3&lt;/code&gt;, not &lt;code&gt;3.333&lt;/code&gt;. If you need decimal math, use &lt;code&gt;bc&lt;/code&gt; or &lt;code&gt;awk&lt;/code&gt; — we'll touch on those in later parts of this series.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Storing Arithmetic Results
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="nv"&gt;width&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;8
&lt;span class="nv"&gt;height&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;5

&lt;span class="nv"&gt;area&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;$((&lt;/span&gt;width &lt;span class="o"&gt;*&lt;/span&gt; height&lt;span class="k"&gt;))&lt;/span&gt;
&lt;span class="nv"&gt;perimeter&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;$((&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;width &lt;span class="o"&gt;+&lt;/span&gt; height&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;))&lt;/span&gt;

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Width: &lt;/span&gt;&lt;span class="nv"&gt;$width&lt;/span&gt;&lt;span class="s2"&gt;, Height: &lt;/span&gt;&lt;span class="nv"&gt;$height&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Area: &lt;/span&gt;&lt;span class="nv"&gt;$area&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Perimeter: &lt;/span&gt;&lt;span class="nv"&gt;$perimeter&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Width: 8, Height: 5
Area: 40
Perimeter: 26
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Increment and Decrement
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="nv"&gt;count&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0

&lt;span class="o"&gt;((&lt;/span&gt;count++&lt;span class="o"&gt;))&lt;/span&gt;    &lt;span class="c"&gt;# Increment by 1&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$count&lt;/span&gt;    &lt;span class="c"&gt;# 1&lt;/span&gt;

&lt;span class="o"&gt;((&lt;/span&gt;count +&lt;span class="o"&gt;=&lt;/span&gt; 5&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="c"&gt;# Add 5&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$count&lt;/span&gt;    &lt;span class="c"&gt;# 6&lt;/span&gt;

&lt;span class="o"&gt;((&lt;/span&gt;count--&lt;span class="o"&gt;))&lt;/span&gt;    &lt;span class="c"&gt;# Decrement by 1&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$count&lt;/span&gt;    &lt;span class="c"&gt;# 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Putting It All Together: A Small Practical Script
&lt;/h2&gt;

&lt;p&gt;Let's combine everything we've covered — variables, arguments, user input, and arithmetic — into one cohesive script.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scenario:&lt;/strong&gt; You're building a simple tool for a gym management system. It takes a member's name and monthly fee as arguments, asks how many months they're signing up for, and calculates the total cost.&lt;/p&gt;

&lt;p&gt;Create a file called &lt;code&gt;gym_membership.sh&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="c"&gt;# ─────────────────────────────────────────&lt;/span&gt;
&lt;span class="c"&gt;# gym_membership.sh&lt;/span&gt;
&lt;span class="c"&gt;# Usage: ./gym_membership.sh &amp;lt;name&amp;gt; &amp;lt;monthly_fee&amp;gt;&lt;/span&gt;
&lt;span class="c"&gt;# ─────────────────────────────────────────&lt;/span&gt;

&lt;span class="c"&gt;# Grab arguments&lt;/span&gt;
&lt;span class="nv"&gt;member_name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;1&lt;/span&gt;&lt;span class="k"&gt;:-&lt;/span&gt;&lt;span class="s2"&gt;"Member"&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;
&lt;span class="nv"&gt;monthly_fee&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;2&lt;/span&gt;&lt;span class="k"&gt;:-&lt;/span&gt;&lt;span class="nv"&gt;0&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"========================================="&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"       Gym Membership Calculator"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"========================================="&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Welcome, &lt;/span&gt;&lt;span class="nv"&gt;$member_name&lt;/span&gt;&lt;span class="s2"&gt;!"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Monthly fee: ₹&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;monthly_fee&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;

&lt;span class="c"&gt;# Ask for duration&lt;/span&gt;
&lt;span class="nb"&gt;read&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; &lt;span class="s2"&gt;"How many months are you signing up for? "&lt;/span&gt; months

&lt;span class="c"&gt;# Calculate totals&lt;/span&gt;
&lt;span class="nv"&gt;total&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;$((&lt;/span&gt;monthly_fee &lt;span class="o"&gt;*&lt;/span&gt; months&lt;span class="k"&gt;))&lt;/span&gt;
&lt;span class="nv"&gt;annual_equivalent&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;$((&lt;/span&gt;monthly_fee &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="m"&gt;12&lt;/span&gt;&lt;span class="k"&gt;))&lt;/span&gt;

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"-----------------------------------------"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"  Membership Summary"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"-----------------------------------------"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"  Name:               &lt;/span&gt;&lt;span class="nv"&gt;$member_name&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"  Monthly Fee:        ₹&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;monthly_fee&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"  Duration:           &lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;months&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; months"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"  Total Cost:         ₹&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;total&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"  (Annual would be:   ₹&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;annual_equivalent&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;)"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"-----------------------------------------"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Registration complete. See you at the gym!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make it executable and run it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;chmod&lt;/span&gt; +x gym_membership.sh
./gym_membership.sh Malhar 999
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sample output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;=========================================
       Gym Membership Calculator
=========================================

Welcome, Malhar!
Monthly fee: ₹999

How many months are you signing up for? 6

-----------------------------------------
  Membership Summary
-----------------------------------------
  Name:               Malhar
  Monthly Fee:        ₹999
  Duration:           6 months
  Total Cost:         ₹5994
  (Annual would be:   ₹11988)
-----------------------------------------

Registration complete. See you at the gym!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a real, working script. Not a toy. You could drop this into a server right now and use it.&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Let's recap what you've learned in Part 1:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;What a shell script is&lt;/strong&gt; — a plain text file with commands that the shell executes line by line&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The shebang (&lt;code&gt;#!/bin/bash&lt;/code&gt;)&lt;/strong&gt; — tells the OS which interpreter to use&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Creating and running scripts&lt;/strong&gt; — &lt;code&gt;touch&lt;/code&gt;, &lt;code&gt;chmod +x&lt;/code&gt;, and &lt;code&gt;./script.sh&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Variables&lt;/strong&gt; — declaring with &lt;code&gt;name="value"&lt;/code&gt;, referencing with &lt;code&gt;$name&lt;/code&gt; or &lt;code&gt;${name}&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Command line arguments&lt;/strong&gt; — &lt;code&gt;$0&lt;/code&gt;, &lt;code&gt;$1&lt;/code&gt;, &lt;code&gt;$2&lt;/code&gt;, &lt;code&gt;$#&lt;/code&gt;, and default values with &lt;code&gt;${1:-default}&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;User input&lt;/strong&gt; — &lt;code&gt;read&lt;/code&gt;, &lt;code&gt;read -p&lt;/code&gt;, and &lt;code&gt;read -s&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Arithmetic&lt;/strong&gt; — &lt;code&gt;$(( ))&lt;/code&gt; for clean integer math&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are the building blocks. Every shell script you'll ever write — no matter how complex — is made up of some combination of these fundamentals.&lt;/p&gt;




&lt;h2&gt;
  
  
  What's Coming in Part 2
&lt;/h2&gt;

&lt;p&gt;We've only just scratched the surface. In Part 2, we're going to add the real logic that turns scripts from simple command sequences into actual automation tools:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Conditional logic&lt;/strong&gt; — &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;elif&lt;/code&gt;, &lt;code&gt;else&lt;/code&gt; to make decisions&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Loops&lt;/strong&gt; — &lt;code&gt;for&lt;/code&gt; and &lt;code&gt;while&lt;/code&gt; to repeat actions&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Case statements&lt;/strong&gt; — cleaner multi-branch logic&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Functions&lt;/strong&gt; — reusable blocks of code&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Exit codes&lt;/strong&gt; — how scripts communicate success or failure&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Real automation scripts&lt;/strong&gt; — backing up directories, monitoring disk usage, batch processing files&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By the end of Part 2, you'll have scripts worth actually putting into a cron job.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Found this helpful? Drop a like or a comment — it genuinely helps. And if you're following along and something isn't working the way you expect, ask in the comments. Shell scripting has some gotchas that trip everyone up the first time.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;See you in Part 2.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>linux</category>
      <category>devops</category>
      <category>bash</category>
    </item>
    <item>
      <title>My Computer Networks Journey - From Confusion to Clarity with Kunal Kushwaha</title>
      <dc:creator>Malhar Gupte</dc:creator>
      <pubDate>Wed, 10 Sep 2025 15:39:04 +0000</pubDate>
      <link>https://dev.to/guptem/my-computer-networks-journey-from-confusion-to-clarity-with-kunal-kushwaha-320b</link>
      <guid>https://dev.to/guptem/my-computer-networks-journey-from-confusion-to-clarity-with-kunal-kushwaha-320b</guid>
      <description>&lt;p&gt;Starting a deep dive into computer networks felt like trying to understand an invisible world that somehow makes everything work. Over the past three weeks, I've been working through Kunal Kushwaha's comprehensive Computer Networks course, and what a journey it's been! As someone who always wondered "how does the internet actually work?", I can finally say I have real answers.&lt;/p&gt;

&lt;p&gt;Let me share everything I learned and why understanding networks is absolutely crucial for any developer or tech enthusiast.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Computer Networks - The Invisible Infrastructure
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What I Thought Networks Were vs. Reality
&lt;/h3&gt;

&lt;p&gt;Before this course, my understanding of networks was embarrassingly surface-level:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"Wi-Fi connects me to the internet somehow"&lt;/li&gt;
&lt;li&gt;"Routers do... router things?"&lt;/li&gt;
&lt;li&gt;"HTTP is just how websites work"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now I realize that computer networks are the entire foundation of modern digital life. Every app, every website, every cloud service, every smart device - they all depend on the same fundamental networking principles that I finally understand.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Problems Networks Solve
&lt;/h3&gt;

&lt;p&gt;Kunal's course brilliantly explained why we need networks in the first place:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Without Networks:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Computers would be isolated islands&lt;/li&gt;
&lt;li&gt;No sharing of resources or information&lt;/li&gt;
&lt;li&gt;Every computer would need its own printer, storage, etc.&lt;/li&gt;
&lt;li&gt;No internet, no cloud, no modern computing as we know it&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;With Networks:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Resource sharing across the globe&lt;/li&gt;
&lt;li&gt;Distributed computing and cloud services&lt;/li&gt;
&lt;li&gt;Real-time communication and collaboration&lt;/li&gt;
&lt;li&gt;Scalable, reliable systems&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Protocol Stack - OSI vs TCP/IP Models
&lt;/h2&gt;

&lt;h3&gt;
  
  
  OSI Model (7 Layers) - The Theoretical Framework
&lt;/h3&gt;

&lt;p&gt;Kunal's explanation of the OSI model was incredibly detailed:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Layer 7 - Application Layer&lt;/strong&gt;: User interfaces (HTTP, SMTP, FTP)&lt;br&gt;
&lt;strong&gt;Layer 6 - Presentation Layer&lt;/strong&gt;: Data formatting, encryption, compression&lt;br&gt;
&lt;strong&gt;Layer 5 - Session Layer&lt;/strong&gt;: Managing sessions between applications&lt;br&gt;
&lt;strong&gt;Layer 4 - Transport Layer&lt;/strong&gt;: Reliable delivery (TCP/UDP)&lt;br&gt;
&lt;strong&gt;Layer 3 - Network Layer&lt;/strong&gt;: Routing between networks (IP)&lt;br&gt;
&lt;strong&gt;Layer 2 - Data Link Layer&lt;/strong&gt;: Local network communication (Ethernet)&lt;br&gt;
&lt;strong&gt;Layer 1 - Physical Layer&lt;/strong&gt;: Physical transmission (cables, radio waves)&lt;/p&gt;

&lt;h3&gt;
  
  
  TCP/IP Model (5 Layers) - The Practical Reality
&lt;/h3&gt;

&lt;p&gt;The TCP/IP model is what's actually implemented:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Application Layer&lt;/strong&gt;: Combines OSI layers 5, 6, and 7&lt;br&gt;
&lt;strong&gt;4. Transport Layer&lt;/strong&gt;: TCP/UDP protocols&lt;br&gt;
&lt;strong&gt;3. Network Layer&lt;/strong&gt;: IP routing&lt;br&gt;
&lt;strong&gt;2. Data Link Layer&lt;/strong&gt;: Ethernet, Wi-Fi&lt;br&gt;
&lt;strong&gt;1. Physical Layer&lt;/strong&gt;: Hardware transmission&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Both Models Matter
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;OSI: Great for understanding concepts and troubleshooting&lt;/li&gt;
&lt;li&gt;TCP/IP: What actually runs the internet&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Networking Devices Deep Dive
&lt;/h2&gt;

&lt;p&gt;Understanding the role of each device was crucial:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hubs&lt;/strong&gt;: Dumb devices that repeat signals (mostly obsolete)&lt;br&gt;
&lt;strong&gt;Switches&lt;/strong&gt;: Smart devices that learn MAC addresses&lt;br&gt;
&lt;strong&gt;Routers&lt;/strong&gt;: Connect different networks using IP addresses&lt;br&gt;
&lt;strong&gt;Gateways&lt;/strong&gt;: Protocol converters between different network types&lt;br&gt;
&lt;strong&gt;Firewalls&lt;/strong&gt;: Security devices that filter traffic&lt;br&gt;
&lt;strong&gt;Load Balancers&lt;/strong&gt;: Distribute traffic across multiple servers&lt;/p&gt;

&lt;h2&gt;
  
  
  Sockets and Ports - The Communication Endpoints
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Sockets - The Programming Interface
&lt;/h3&gt;

&lt;p&gt;A socket is essentially:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;IP Address + Port Number + Protocol&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;The endpoint for network communication&lt;/li&gt;
&lt;li&gt;What programmers use to create network applications&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example: &lt;code&gt;192.168.1.100:8080&lt;/code&gt; using TCP&lt;/p&gt;

&lt;h3&gt;
  
  
  Port Categories I Learned About
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Well-Known Ports (0-1023):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reserved for system services&lt;/li&gt;
&lt;li&gt;Require admin privileges to use&lt;/li&gt;
&lt;li&gt;Examples: HTTP (80), HTTPS (443), SSH (22)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Registered Ports (1024-49151):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Used by user applications&lt;/li&gt;
&lt;li&gt;Examples: MySQL (3306), PostgreSQL (5432)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Dynamic/Private Ports (49152-65535):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Used for client connections&lt;/li&gt;
&lt;li&gt;Assigned automatically by the operating system&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  HTTP Protocol Deep Dive
&lt;/h2&gt;

&lt;h3&gt;
  
  
  HTTP Methods That Actually Make Sense Now
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;GET&lt;/strong&gt;: "Give me this resource" (idempotent, safe)&lt;br&gt;
&lt;strong&gt;POST&lt;/strong&gt;: "Create something new" (not idempotent)&lt;br&gt;
&lt;strong&gt;PUT&lt;/strong&gt;: "Replace this entire resource" (idempotent)&lt;br&gt;
&lt;strong&gt;PATCH&lt;/strong&gt;: "Update part of this resource"&lt;br&gt;
&lt;strong&gt;DELETE&lt;/strong&gt;: "Remove this resource" (idempotent)&lt;/p&gt;

&lt;h3&gt;
  
  
  HTTP Status Codes I Now Understand
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;2xx Success:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;200 OK: Everything worked&lt;/li&gt;
&lt;li&gt;201 Created: New resource created&lt;/li&gt;
&lt;li&gt;204 No Content: Success, but no data to return&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3xx Redirection:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;301 Moved Permanently: Resource has new permanent location&lt;/li&gt;
&lt;li&gt;302 Found: Temporary redirect&lt;/li&gt;
&lt;li&gt;304 Not Modified: Use cached version&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4xx Client Errors:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;400 Bad Request: Malformed request&lt;/li&gt;
&lt;li&gt;401 Unauthorized: Authentication required&lt;/li&gt;
&lt;li&gt;403 Forbidden: Access denied&lt;/li&gt;
&lt;li&gt;404 Not Found: Resource doesn't exist&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;5xx Server Errors:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;500 Internal Server Error: Something broke on the server&lt;/li&gt;
&lt;li&gt;502 Bad Gateway: Upstream server issue&lt;/li&gt;
&lt;li&gt;503 Service Unavailable: Server temporarily down&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Cookies - Session Management Made Simple
&lt;/h3&gt;

&lt;p&gt;Cookies finally made sense:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Server sends &lt;code&gt;Set-Cookie&lt;/code&gt; header&lt;/li&gt;
&lt;li&gt;Browser stores the cookie&lt;/li&gt;
&lt;li&gt;Browser sends cookie with subsequent requests&lt;/li&gt;
&lt;li&gt;Server uses cookie to maintain state&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This enables shopping carts, login sessions, and personalization.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Email Actually Works
&lt;/h2&gt;

&lt;p&gt;This was one of my favorite sections! The email journey:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Compose&lt;/strong&gt;: You write an email in your client&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SMTP&lt;/strong&gt;: Your client sends to your email server&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DNS&lt;/strong&gt;: Server looks up recipient's email server&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SMTP&lt;/strong&gt;: Your server sends to recipient's server&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Storage&lt;/strong&gt;: Recipient's server stores the email&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;POP3/IMAP&lt;/strong&gt;: Recipient downloads via email client&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Key Protocols:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SMTP&lt;/strong&gt;: Sending emails (Simple Mail Transfer Protocol)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;POP3&lt;/strong&gt;: Downloading emails (deletes from server)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;IMAP&lt;/strong&gt;: Accessing emails (keeps on server)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  DNS - The Internet's Phone Book
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How Domain Name Resolution Works
&lt;/h3&gt;

&lt;p&gt;When you type &lt;code&gt;www.google.com&lt;/code&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Browser Cache&lt;/strong&gt;: Check local cache first&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;OS Cache&lt;/strong&gt;: Check operating system cache&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Router Cache&lt;/strong&gt;: Check home router cache&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ISP DNS&lt;/strong&gt;: Query internet service provider&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Root Servers&lt;/strong&gt;: Query root name servers (.)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;TLD Servers&lt;/strong&gt;: Query top-level domain servers (.com)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Authoritative Servers&lt;/strong&gt;: Query Google's name servers&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Response&lt;/strong&gt;: IP address returned through the chain&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This complex process happens in &lt;strong&gt;milliseconds&lt;/strong&gt;!&lt;/p&gt;

&lt;h3&gt;
  
  
  DNS Record Types
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A Record&lt;/strong&gt;: Maps domain to IPv4 address&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AAAA Record&lt;/strong&gt;: Maps domain to IPv6 address&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CNAME&lt;/strong&gt;: Creates an alias for another domain&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MX Record&lt;/strong&gt;: Specifies email servers&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;TXT Record&lt;/strong&gt;: Stores text information&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Transport Layer Deep Dive
&lt;/h2&gt;

&lt;h3&gt;
  
  
  TCP (Transmission Control Protocol) - The Reliable Choice
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Key Features:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Connection-oriented&lt;/strong&gt;: Establishes connection before data transfer&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reliable&lt;/strong&gt;: Guarantees data delivery&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ordered&lt;/strong&gt;: Data arrives in correct sequence&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Error detection and correction&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flow control&lt;/strong&gt;: Prevents overwhelming the receiver&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Famous 3-Way Handshake
&lt;/h3&gt;

&lt;p&gt;This elegant dance establishes TCP connections:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;SYN&lt;/strong&gt;: Client sends synchronization request&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SYN-ACK&lt;/strong&gt;: Server acknowledges and sends its sync&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ACK&lt;/strong&gt;: Client acknowledges server's sync&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Connection established! Now data can flow reliably.&lt;/p&gt;

&lt;h3&gt;
  
  
  UDP (User Datagram Protocol) - The Fast and Furious
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Key Features:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Connectionless&lt;/strong&gt;: No handshake required&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unreliable&lt;/strong&gt;: No guarantee of delivery&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fast&lt;/strong&gt;: Minimal overhead&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unordered&lt;/strong&gt;: Data can arrive out of sequence&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Perfect for:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Video streaming (occasional lost frames are okay)&lt;/li&gt;
&lt;li&gt;Online gaming (speed over perfection)&lt;/li&gt;
&lt;li&gt;DNS queries (fast lookups)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Checksums and Error Detection
&lt;/h3&gt;

&lt;p&gt;Every TCP/UDP packet includes a checksum:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mathematical calculation based on data content&lt;/li&gt;
&lt;li&gt;Receiver recalculates and compares&lt;/li&gt;
&lt;li&gt;If checksums don't match, data is corrupted&lt;/li&gt;
&lt;li&gt;TCP retransmits, UDP discards&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Timers in TCP
&lt;/h3&gt;

&lt;p&gt;TCP uses various timers for reliability:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Retransmission Timer&lt;/strong&gt;: Resends if no acknowledgment&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep-Alive Timer&lt;/strong&gt;: Maintains idle connections&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Time-Wait Timer&lt;/strong&gt;: Ensures clean connection closure&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Network Layer - Routing and IP
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Control Plane vs Data Plane
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Control Plane:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Makes routing decisions&lt;/li&gt;
&lt;li&gt;Builds routing tables&lt;/li&gt;
&lt;li&gt;Exchanges routing information&lt;/li&gt;
&lt;li&gt;The "brain" of the network&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Data Plane:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Forwards actual packets&lt;/li&gt;
&lt;li&gt;Uses routing table decisions&lt;/li&gt;
&lt;li&gt;The "muscle" of the network&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  IP (Internet Protocol) - The Universal Language
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;IPv4 Packet Structure:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Version, Header Length, Type of Service&lt;/li&gt;
&lt;li&gt;Total Length, Identification, Flags&lt;/li&gt;
&lt;li&gt;Fragment Offset, Time to Live, Protocol&lt;/li&gt;
&lt;li&gt;Header Checksum, Source IP, Destination IP&lt;/li&gt;
&lt;li&gt;Options, Data&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Packets vs Frames vs Segments
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Segments&lt;/strong&gt;: Transport layer (TCP/UDP)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Packets&lt;/strong&gt;: Network layer (IP)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Frames&lt;/strong&gt;: Data link layer (Ethernet)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each layer adds its own header!&lt;/p&gt;

&lt;h3&gt;
  
  
  Middle Boxes - The Network Helpers
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;NAT (Network Address Translation):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Allows multiple devices to share one public IP&lt;/li&gt;
&lt;li&gt;Translates private IPs (192.168.x.x) to public IP&lt;/li&gt;
&lt;li&gt;Essential for home networks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Firewalls:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Filter traffic based on rules&lt;/li&gt;
&lt;li&gt;Can operate at multiple layers&lt;/li&gt;
&lt;li&gt;Protect networks from threats&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Load Balancers:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Distribute traffic across servers&lt;/li&gt;
&lt;li&gt;Improve performance and reliability&lt;/li&gt;
&lt;li&gt;Can detect server failures&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Data Link Layer - The Local Network Champion
&lt;/h2&gt;

&lt;p&gt;This layer handles communication within the same network segment:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;MAC Addresses&lt;/strong&gt;: Physical hardware addresses&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ethernet&lt;/strong&gt;: Most common wired protocol&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Wi-Fi&lt;/strong&gt;: Wireless local area networking&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Switches&lt;/strong&gt;: Forward frames based on MAC addresses&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The course covered how frames are constructed and how switches learn MAC addresses to build their forwarding tables.&lt;/p&gt;

&lt;h2&gt;
  
  
  Network Security Fundamentals
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Threats I Never Considered
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Man-in-the-Middle Attacks&lt;/strong&gt;: Intercepting communication between two parties&lt;br&gt;
&lt;strong&gt;DNS Spoofing&lt;/strong&gt;: Redirecting domain names to malicious servers&lt;br&gt;
&lt;strong&gt;DDoS Attacks&lt;/strong&gt;: Overwhelming servers with traffic&lt;br&gt;
&lt;strong&gt;Packet Sniffing&lt;/strong&gt;: Reading unencrypted network traffic&lt;/p&gt;

&lt;h3&gt;
  
  
  Security Measures That Make Sense Now
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Firewalls&lt;/strong&gt;: Like bouncers for network traffic&lt;br&gt;
&lt;strong&gt;VPNs&lt;/strong&gt;: Creating secure tunnels through insecure networks&lt;br&gt;
&lt;strong&gt;SSL/TLS&lt;/strong&gt;: Encrypting web traffic&lt;br&gt;
&lt;strong&gt;Network Segmentation&lt;/strong&gt;: Isolating sensitive systems&lt;/p&gt;

&lt;h2&gt;
  
  
  Routing and Switching - The Traffic Controllers
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How Data Finds Its Way
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Routers&lt;/strong&gt;: Make decisions about where to send data across different networks&lt;br&gt;
&lt;strong&gt;Switches&lt;/strong&gt;: Handle communication within a single network&lt;br&gt;
&lt;strong&gt;Routing Tables&lt;/strong&gt;: Like GPS for network packets&lt;/p&gt;

&lt;h3&gt;
  
  
  Network Topologies I Finally Understand
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Star Topology&lt;/strong&gt;: Everything connects to a central hub&lt;br&gt;
&lt;strong&gt;Ring Topology&lt;/strong&gt;: Devices connected in a circular chain&lt;br&gt;
&lt;strong&gt;Mesh Topology&lt;/strong&gt;: Every device connected to every other device&lt;br&gt;
&lt;strong&gt;Hybrid Topologies&lt;/strong&gt;: Real-world combinations of the above&lt;/p&gt;

&lt;h2&gt;
  
  
  Practical Applications That Clicked
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Load Balancing
&lt;/h3&gt;

&lt;p&gt;Distributing traffic across multiple servers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Round Robin&lt;/strong&gt;: Take turns serving requests&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Least Connections&lt;/strong&gt;: Send to the least busy server&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Geographic&lt;/strong&gt;: Send to the nearest server&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Content Delivery Networks (CDNs)
&lt;/h3&gt;

&lt;p&gt;Now I understand why Netflix works so well globally - they store content close to users in hundreds of locations worldwide.&lt;/p&gt;

&lt;h3&gt;
  
  
  Network Address Translation (NAT)
&lt;/h3&gt;

&lt;p&gt;How your router allows multiple devices to share one public IP address - this explained so much about home networking!&lt;/p&gt;

&lt;h2&gt;
  
  
  Tools and Commands I Learned
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Essential Network Commands
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Check connectivity&lt;/span&gt;
ping google.com

&lt;span class="c"&gt;# Trace route to destination&lt;/span&gt;
traceroute google.com

&lt;span class="c"&gt;# Check DNS resolution&lt;/span&gt;
nslookup google.com

&lt;span class="c"&gt;# Display network configuration&lt;/span&gt;
ifconfig &lt;span class="o"&gt;(&lt;/span&gt;Linux/Mac&lt;span class="o"&gt;)&lt;/span&gt; or ipconfig &lt;span class="o"&gt;(&lt;/span&gt;Windows&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="c"&gt;# Show network connections&lt;/span&gt;
netstat &lt;span class="nt"&gt;-an&lt;/span&gt;

&lt;span class="c"&gt;# Display routing table&lt;/span&gt;
route &lt;span class="nt"&gt;-n&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Network Analysis Tools
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Wireshark&lt;/strong&gt;: Capture and analyze network packets&lt;br&gt;
&lt;strong&gt;Nmap&lt;/strong&gt;: Network discovery and security scanning&lt;br&gt;
&lt;strong&gt;Curl&lt;/strong&gt;: Test HTTP requests from command line&lt;br&gt;
&lt;strong&gt;Telnet&lt;/strong&gt;: Test network connections&lt;/p&gt;

&lt;h2&gt;
  
  
  How This Connects to Development
&lt;/h2&gt;

&lt;h3&gt;
  
  
  API Design Makes More Sense
&lt;/h3&gt;

&lt;p&gt;Understanding HTTP methods, status codes, and headers helps me design better APIs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;200: Success&lt;/li&gt;
&lt;li&gt;404: Not Found&lt;/li&gt;
&lt;li&gt;500: Server Error&lt;/li&gt;
&lt;li&gt;401: Unauthorized&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Database Connections
&lt;/h3&gt;

&lt;p&gt;Network concepts explain database connection pooling, timeouts, and distributed databases.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cloud Computing
&lt;/h3&gt;

&lt;p&gt;AWS, Azure, GCP services all make more sense when you understand VPCs, subnets, security groups, and load balancers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Microservices Architecture
&lt;/h3&gt;

&lt;p&gt;Service discovery, API gateways, and inter-service communication rely heavily on networking fundamentals.&lt;/p&gt;

&lt;h2&gt;
  
  
  Challenges and Aha Moments
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What Was Difficult
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Binary and Hexadecimal Math&lt;/strong&gt;: Converting between number systems for subnet calculations&lt;br&gt;
&lt;strong&gt;Protocol Stack Interactions&lt;/strong&gt;: Understanding how all the layers work together&lt;br&gt;
&lt;strong&gt;Network Troubleshooting&lt;/strong&gt;: Learning systematic approaches to diagnose issues&lt;/p&gt;

&lt;h3&gt;
  
  
  Breakthrough Moments
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Subnet Masking&lt;/strong&gt;: When I finally understood how to calculate network ranges&lt;br&gt;
&lt;strong&gt;Three-Way Handshake&lt;/strong&gt;: The elegant simplicity of TCP connection establishment&lt;br&gt;
&lt;strong&gt;DNS Hierarchy&lt;/strong&gt;: Realizing the internet is basically a giant distributed database&lt;/p&gt;

&lt;h2&gt;
  
  
  Questions for the Community
&lt;/h2&gt;

&lt;p&gt;I'd love to hear from experienced network engineers and developers:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What networking concepts do you use most in your daily work?&lt;/li&gt;
&lt;li&gt;Any recommendations for hands-on networking practice labs?&lt;/li&gt;
&lt;li&gt;Which network monitoring tools do you find most valuable?&lt;/li&gt;
&lt;li&gt;Common networking mistakes you see developers make?&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What's Next in My Learning Journey
&lt;/h2&gt;

&lt;p&gt;Now that I have solid networking fundamentals, I'm currently preparing for the AWS Cloud Practitioner Certification (CLF-C02). The networking concepts from this course are already helping me understand VPCs, security groups, and cloud networking services much better!&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources That Made the Difference
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://youtu.be/IPvYjXCsTg8" rel="noopener noreferrer"&gt;&lt;strong&gt;Kunal Kushwaha's Computer Networks Course&lt;/strong&gt;&lt;/a&gt;: Excellent explanations with real-world examples&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GeeksforGeeks Articles&lt;/strong&gt;: Supplemented the course with detailed articles on specific networking topics and concepts&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Community Support&lt;/strong&gt;: Discord discussions and Q&amp;amp;A sessions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you're considering learning computer networks, I highly recommend Kunal's course. The concepts might seem abstract at first, but once they click, you'll see the network layer in everything you build.&lt;/p&gt;

&lt;p&gt;Understanding networks has made me a better developer, a more effective debugger, and given me confidence to architect distributed systems. It's one of those foundational skills that pays dividends across your entire career.&lt;/p&gt;

&lt;p&gt;What networking concepts do you find most challenging or interesting? Share your experiences in the comments!&lt;/p&gt;




&lt;p&gt;&lt;em&gt;See you next week for more learning adventures!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>networking</category>
      <category>learning</category>
    </item>
    <item>
      <title>My DevOps Journey - Understanding the Why and Linux Fundamentals</title>
      <dc:creator>Malhar Gupte</dc:creator>
      <pubDate>Sun, 17 Aug 2025 13:34:40 +0000</pubDate>
      <link>https://dev.to/guptem/my-devops-journey-understanding-the-why-and-linux-fundamentals-140i</link>
      <guid>https://dev.to/guptem/my-devops-journey-understanding-the-why-and-linux-fundamentals-140i</guid>
      <description>&lt;p&gt;Starting a new learning journey always feels both exciting and overwhelming. This week, I decided to dive into the world of DevOps, and let me tell you - it's been an eye-opening experience. As someone looking to understand this field better, I chose to start with the fundamentals: understanding what DevOps really is and getting comfortable with Linux.&lt;/p&gt;

&lt;p&gt;My learning sources for this week were TechWorld with Nana for DevOps concepts and TrainWithShubham for Linux fundamentals. Here's everything I learned and why it matters.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Understanding DevOps - More Than Just Buzzwords&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What DevOps Actually Is&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before this week, I thought DevOps was just another tech buzzword - maybe some tools, maybe some automation. Boy, was I wrong! Thanks to Nana's clear explanations, I now understand that DevOps is fundamentally about &lt;strong&gt;culture and collaboration&lt;/strong&gt;, not just tools.&lt;/p&gt;

&lt;p&gt;DevOps is a methodology that bridges the gap between Development and Operations teams. It's about breaking down the traditional silos where developers write code and throw it "over the wall" to operations teams who then struggle to deploy and maintain it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Problems DevOps Solves&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The more I learned about traditional software development approaches, the more I understood why DevOps became essential:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Before DevOps:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Deployments took days or weeks&lt;/li&gt;
&lt;li&gt;Development and Operations teams barely communicated&lt;/li&gt;
&lt;li&gt;Testing happened only at the end (too late!)&lt;/li&gt;
&lt;li&gt;Environments were inconsistent ("it works on my machine!")&lt;/li&gt;
&lt;li&gt;Manual processes everywhere = human errors everywhere&lt;/li&gt;
&lt;li&gt;Slow feedback cycles meant issues were discovered late&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;After DevOps:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multiple deployments per day (Netflix deploys thousands of times daily!)&lt;/li&gt;
&lt;li&gt;Teams collaborate throughout the entire lifecycle&lt;/li&gt;
&lt;li&gt;Continuous testing and integration&lt;/li&gt;
&lt;li&gt;Consistent environments through automation&lt;/li&gt;
&lt;li&gt;Automated processes reduce human error&lt;/li&gt;
&lt;li&gt;Fast feedback loops catch issues early&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Key DevOps Principles That Clicked for Me&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Automation Everywhere&lt;/strong&gt;: If you're doing it manually more than once, automate it&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Continuous Integration/Continuous Deployment (CI/CD)&lt;/strong&gt;: Code changes are automatically tested and deployed&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Infrastructure as Code&lt;/strong&gt;: Manage your infrastructure like you manage your application code&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Monitoring and Feedback&lt;/strong&gt;: Constantly monitor and improve based on data&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fail Fast, Recover Quickly&lt;/strong&gt;: It's better to find problems early when they're cheaper to fix&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Linux Fundamentals - The Foundation of Everything
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Why Linux Matters in DevOps&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before diving into commands, I needed to understand why Linux is so crucial for DevOps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Most servers run Linux&lt;/strong&gt; (96% of the top 1 million web servers!)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Container technologies&lt;/strong&gt; like Docker are Linux-based&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cloud platforms&lt;/strong&gt; predominantly use Linux&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Automation scripts&lt;/strong&gt; are often written in shell&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cost-effective&lt;/strong&gt; - no licensing fees unlike Windows servers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Essential Commands I Mastered This Week&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here are the commands I practiced and their real-world applications:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Navigation and File Operations:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Basic navigation&lt;/span&gt;
&lt;span class="nb"&gt;pwd&lt;/span&gt;                    &lt;span class="c"&gt;# Show current directory&lt;/span&gt;
&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-la&lt;/span&gt;                &lt;span class="c"&gt;# List files with details&lt;/span&gt;
&lt;span class="nb"&gt;cd&lt;/span&gt; /path/to/directory &lt;span class="c"&gt;# Change directory&lt;/span&gt;
&lt;span class="nb"&gt;mkdir &lt;/span&gt;project-folder  &lt;span class="c"&gt;# Create directory&lt;/span&gt;
&lt;span class="nb"&gt;rmdir &lt;/span&gt;empty-folder    &lt;span class="c"&gt;# Remove empty directory&lt;/span&gt;
&lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="nt"&gt;-rf&lt;/span&gt; folder         &lt;span class="c"&gt;# Remove directory and contents&lt;/span&gt;

&lt;span class="c"&gt;# File operations&lt;/span&gt;
&lt;span class="nb"&gt;cp source &lt;/span&gt;destination  &lt;span class="c"&gt;# Copy files&lt;/span&gt;
&lt;span class="nb"&gt;mv &lt;/span&gt;oldname newname    &lt;span class="c"&gt;# Move/rename files&lt;/span&gt;
&lt;span class="nb"&gt;cat &lt;/span&gt;filename          &lt;span class="c"&gt;# Display file contents&lt;/span&gt;
less filename         &lt;span class="c"&gt;# View file page by page&lt;/span&gt;
&lt;span class="nb"&gt;head&lt;/span&gt; &lt;span class="nt"&gt;-10&lt;/span&gt; filename     &lt;span class="c"&gt;# Show first 10 lines&lt;/span&gt;
&lt;span class="nb"&gt;tail&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; logfile       &lt;span class="c"&gt;# Follow log file in real-time&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Permissions and Ownership:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;chmod &lt;/span&gt;755 script.sh         &lt;span class="c"&gt;# Make script executable&lt;/span&gt;
&lt;span class="nb"&gt;chmod&lt;/span&gt; +x script.sh          &lt;span class="c"&gt;# Another way to make executable&lt;/span&gt;
&lt;span class="nb"&gt;chown &lt;/span&gt;user:group filename   &lt;span class="c"&gt;# Change file ownership&lt;/span&gt;
&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-l&lt;/span&gt;                       &lt;span class="c"&gt;# View file permissions&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Process Management:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ps aux                      &lt;span class="c"&gt;# Show all running processes&lt;/span&gt;
top                         &lt;span class="c"&gt;# Real-time process monitor&lt;/span&gt;
&lt;span class="nb"&gt;kill &lt;/span&gt;1234                   &lt;span class="c"&gt;# Terminate process with ID 1234&lt;/span&gt;
&lt;span class="nb"&gt;jobs&lt;/span&gt;                        &lt;span class="c"&gt;# Show active jobs&lt;/span&gt;
&lt;span class="nb"&gt;nohup command&lt;/span&gt; &amp;amp;             &lt;span class="c"&gt;# Run command in background&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Understanding the Linux File System
&lt;/h3&gt;

&lt;p&gt;The Linux directory structure finally makes sense to me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;/&lt;/code&gt; - Root directory (everything starts here)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;/home&lt;/code&gt; - User directories (like C:\Users on Windows)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;/etc&lt;/code&gt; - Configuration files (this is huge for DevOps!)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;/var&lt;/code&gt; - Variable data, including logs (crucial for troubleshooting)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;/usr&lt;/code&gt; - User programs and applications&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;/tmp&lt;/code&gt; - Temporary files (cleaned on reboot)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;/opt&lt;/code&gt; - Optional/third-party software&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  File Permissions Deep Dive
&lt;/h3&gt;

&lt;p&gt;This was initially confusing, but now I get it:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Permission Types:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Read (r) = 4&lt;/li&gt;
&lt;li&gt;Write (w) = 2
&lt;/li&gt;
&lt;li&gt;Execute (x) = 1&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Permission Groups:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User (owner)&lt;/li&gt;
&lt;li&gt;Group &lt;/li&gt;
&lt;li&gt;Other (everyone else)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; &lt;code&gt;chmod 755 script.sh&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Owner: 7 (4+2+1) = read, write, execute&lt;/li&gt;
&lt;li&gt;Group: 5 (4+1) = read, execute&lt;/li&gt;
&lt;li&gt;Other: 5 (4+1) = read, execute&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Connecting the Dots - How This All Fits Together
&lt;/h2&gt;

&lt;p&gt;Now I understand why these fundamentals are crucial for DevOps:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Server Management:&lt;/strong&gt; DevOps engineers constantly SSH into Linux servers to deploy applications, check logs, and troubleshoot issues. Without Linux skills, you're stuck.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Automation Scripts:&lt;/strong&gt; Whether it's deployment scripts, backup scripts, or monitoring scripts, they're typically written in bash and run on Linux systems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Container Technology:&lt;/strong&gt; Docker containers are essentially Linux processes. Understanding Linux concepts like processes, file systems, and permissions is essential for containerization.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Infrastructure as Code:&lt;/strong&gt; Tools like Terraform and Ansible manage Linux servers. You need to understand what you're automating.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CI/CD Pipelines:&lt;/strong&gt; Build servers are typically Linux-based. Your deployment scripts need Linux knowledge.&lt;/p&gt;

&lt;h2&gt;
  
  
  Challenges and Learning Moments
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What Was Difficult
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Command Syntax:&lt;/strong&gt; Remembering all the flags and options. Why is it &lt;code&gt;ls -la&lt;/code&gt; and not &lt;code&gt;ls /a&lt;/code&gt;? Getting used to the Unix philosophy took time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Vim Editor:&lt;/strong&gt; I spent 10 minutes trying to exit Vim on my first try! &lt;code&gt;:q!&lt;/code&gt; is now permanently etched in my memory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Permission Numbers:&lt;/strong&gt; Understanding why 755 means what it means required practice and repetition.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;No Safety Net:&lt;/strong&gt; Unlike Windows with "Are you sure?" dialogs, &lt;code&gt;rm -rf /&lt;/code&gt; can destroy everything. The power requires responsibility.&lt;/p&gt;

&lt;h2&gt;
  
  
  Questions for the Community
&lt;/h2&gt;

&lt;p&gt;I'd love to hear from experienced DevOps practitioners:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;What Linux commands do you use most frequently in your daily DevOps work?&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Any beginner-friendly resources for practicing Linux skills in a DevOps context?&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Any common mistakes I should watch out for as I continue this journey?&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you're also starting your DevOps journey, I encourage you to focus on these fundamentals. Don't rush to the flashy tools - master the basics first. Trust me, everything else will build on top of this foundation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Resources mentioned:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;TechWorld with Nana: &lt;a href="https://youtu.be/0yWAtQ6wYNM" rel="noopener noreferrer"&gt;What is DevOps? REALLY understand it | DevOps vs SRE&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;TrainWithShubham: &lt;a href="https://youtu.be/e01GGTKmtpc" rel="noopener noreferrer"&gt;Linux For DevOps In One Shot&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;See you next week for more learning adventures!&lt;/p&gt;

</description>
      <category>devops</category>
      <category>linux</category>
      <category>automation</category>
      <category>ubuntu</category>
    </item>
  </channel>
</rss>
