Master Shell Scripting: From Basics to Advanced Automation
This guide explains what Shell scripts are, their practical uses and limitations, the underlying interpreter mechanism, and provides step‑by‑step examples covering variables, arithmetic and relational operators, strings, arrays, input/output, conditionals, loops, functions, redirection, and automation scripts such as a Git commit helper.
What Is a Shell Script?
A Shell script is a collection of commands interpreted by the Shell, allowing automation of tasks on Windows, Unix, or Linux systems. Mastering Shell scripts simplifies computer operations and saves time.
What Shell Can Do
Simplify complex commands (e.g., compress multiple Git steps into one).
Automate SDK updates in projects.
Automate packaging, compilation, and deployment.
Clean empty directories.
Any repetitive, rule‑based task can be scripted.
What Shell Cannot Do
Precise calculations.
High‑performance language tasks.
Complex network operations.
It is not a replacement for high‑level programming languages.
How Shell Works
Shell scripts are interpreted rather than compiled, which makes them slightly slower than compiled languages but highly portable.
Simple Script Example
#!/bin/bash
echo "Hello World"Save the file as test.sh, give it execute permission, and run it.
Variables
myText="hello world"
muNum=100Note: No spaces around the = sign.
Accessing Variables
myText="hello world"
muNum=100
echo $myText
echo $muNumUse $ to retrieve the variable's value.
Arithmetic Operators
+ : addition
- : subtraction
* : multiplication (requires escaping)
/ : division
% : modulus
#!/bin/bash
a=3
b=5
val=`expr $a + $b`
echo "Total value : $val"
# Repeat for -, *, /, %Relational Operators
-eq : equal
-ne : not equal
-gt : greater than
-lt : less than
-ge : greater or equal
-le : less or equal
String Operators
= : strings equal
!= : strings not equal
-z : zero length
-n : non‑zero length
File Test Operators
-d : is a directory
-r : readable
-w : writable
-x : executable
-s : non‑empty
-e : exists
Arrays
#!/bin/sh
array=(1 2 3 4 5)
array2=(aa bb cc dd ee)
value=${array[3]}
echo $value
len=${#array[*]}
echo $lenOutput Commands
#!/bin/sh
echo "hello world"
echo -e "hello world"
echo "$(date)"printf
Works like C's printf for formatted output.
Conditional Statements
#!/bin/sh
a=10
b=20
if [ $a == $b ]; then
echo "true"
else
echo "false"
fi
if [ $a -gt $b ]; then
echo "a is greater than b"
elif [ $a -lt $b ]; then
echo "a is less than b"
else
echo "none of the conditions met"
fiLoops
for Loop
#!/bin/sh
for i in {1..5}; do
echo $i
donewhile Loop
#!/bin/sh
COUNTER=0
while [ $COUNTER -lt 5 ]; do
COUNTER=`expr $COUNTER + 1`
echo $COUNTER
doneAnother example reads user input until EOF.
Break and Continue
break # exit all loops
break n # exit n levels
continue # skip to next iterationFunctions
#!/bin/sh
sysout(){
echo "hello world"
}
sysout #!/bin/sh
test(){
aNum=3
anotherNum=5
return $((aNum+anotherNum))
}
test
result=$?
echo $result #!/bin/sh
test(){
echo $1
echo $2
echo $3
echo $#
echo $*
}
test aa bb ccRedirection
echo result > file # overwrite
echo result >> file # append
cat < file # input redirectionAutomation Example: Git Commit Script
#!/bin/bash
echo "-------Begin-------"
git add .
git commit -m $1
echo $1
git push origin master
echo "--------End--------"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.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
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.
