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.
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=100No 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
doneWhile 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"
doneThe 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 iterationFunctions
#!/bin/sh
sysout() { echo "hello world"; }
sysoutFunction without a return value.
#!/bin/sh
test() {
aNum=3
anotherNum=5
return $((aNum + anotherNum))
}
test
result=$?
echo $resultFunction 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 ccFunction that receives parameters.
Redirection
result > file # overwrite file
result >> file # append to file
input < file # read from fileAutomating 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
