Operations 10 min read

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.

21CTO
21CTO
21CTO
Master Shell Scripting: From Basics to Advanced Automation

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=100

Note: No spaces around the = sign.

Accessing Variables

myText="hello world"
muNum=100
echo $myText
echo $muNum

Use $ 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 $len

Output 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"
fi

Loops

for Loop

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

while Loop

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

Another example reads user input until EOF.

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
#!/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 cc

Redirection

echo result > file      # overwrite
echo result >> file     # append
cat < file               # input redirection

Automation Example: Git Commit Script

#!/bin/bash
echo "-------Begin-------"
git add .
git commit -m $1
echo $1
git push origin master
echo "--------End--------"
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.

ShellBashVariablesLoops
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

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.