Fundamentals 11 min read

Master Shell Scripting: Basics, Variables, Loops, and Automation

This guide introduces shell scripting fundamentals, covering its purpose, what it can and cannot do, the interpreter workflow, and detailed examples of simple scripts, variables, arithmetic, string and file tests, control structures, functions, redirection, and an automated GitHub commit script.

ITPUB
ITPUB
ITPUB
Master Shell Scripting: Basics, Variables, Loops, and Automation

Overview

Shell scripts are plain‑text files that contain a sequence of commands interpreted by a shell. They run on Windows, Unix and Linux and are used to automate repetitive or complex command sequences.

Typical Use Cases

Simplify multi‑step operations (e.g., compress a Git commit into a single command).

Automatically replace SDK/library versions in a project.

Automate packaging, compilation and deployment.

Clean empty directories.

Any regular, repeatable task that can be expressed as shell commands.

Limitations

Heavy numeric computations.

Scenarios requiring high‑performance language efficiency.

Complex network operations.

Shell is not a replacement for high‑level programming languages.

Execution Model

Shell is an interpreted scripting language; the interpreter reads the script line‑by‑line and executes each command. This makes it slower than compiled languages but very flexible for quick automation.

Hello‑World Example

#!/bin/bash
echo "Hello World"

Save the file as test.sh, make it executable with chmod +x test.sh, and run it with ./test.sh.

Variables

myText="hello world"
myNum=100

No spaces are allowed around the =. Access a variable by prefixing it with $, e.g., echo $myText.

Arithmetic Operations

Supported operators are +, -, *, /. Spaces are required around the operator, and * must be escaped.

#!/bin/bash
a=3
b=5
val=$(expr $a + $b)
echo "Total value : $val"
val=$(expr $a - $b)
echo "Total value : $val"
val=$(expr $a \* $b)
echo "Total value : $val"
val=$(expr $a / $b)
echo "Total value : $val"

Additional Operators

%

– modulo == – equality (string) = – assignment != – inequality ! – logical NOT -o – logical OR -a – logical AND

Relational Operators (numeric)

-eq

– equal -ne – not equal -gt – greater than -lt – less than -ge – greater than or equal -le – less than or equal

String Operators

=

– strings are equal != – strings are not equal -z – string length is zero -n – string length is non‑zero

File Test Operators

-d file

– is a directory -r file – is readable -w file – is writable -x file – is executable -s file – is non‑empty -e file – exists

Loops

For Loop

#!/bin/sh
for i in {1..5}; do
  echo $i
done

for i in 5 6 7 8 9; do
  echo $i
done

for FILE in $HOME/.bash*; do
  echo $FILE
done

While Loop

#!/bin/sh
COUNTER=0
while [ $COUNTER -lt 5 ]; do
  COUNTER=$(expr $COUNTER + 1)
  echo $COUNTER
done

while read FILM; do
  echo "Yeah! great film the $FILM"
done

The second form reads user input from stdin until EOF (Ctrl‑D).

Break and Continue

break        # exit all loops
break n      # exit n levels
continue     # skip to next iteration

Functions

#!/bin/sh
sysout() { echo "hello world"; }
sysout

Function without a return value.

#!/bin/sh
test() {
  aNum=3
  anotherNum=5
  return $((aNum + anotherNum))
}
test
result=$?
echo $result

Function returning a numeric status.

#!/bin/sh
test() {
  echo $1   # first argument
  echo $2   # second argument
  echo $3   # third argument
  echo $#   # number of arguments
  echo $*   # all arguments
}
test aa bb cc

Function that receives parameters.

Redirection

result > file      # overwrite file
result >> file     # append to file
input < file        # read from file

Automating GitHub Commits

#!/bin/bash
echo "-------Begin-------"
git add .
git commit -m "$1"
echo "$1"
git push origin master
echo "--------End--------"

This script stages all changes, creates a commit with the message supplied as the first argument, and pushes to the master branch.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Shellcommand-lineScripting
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.