Operations 23 min read

Master Shell Scripting: From Basics to Advanced Techniques

This comprehensive guide introduces shell programming fundamentals, demonstrates how to write, execute, and manage scripts, explains variables, strings, parameters, arithmetic, control structures, functions, arrays, and file inclusion, and provides practical code examples for Linux environments.

ITPUB
ITPUB
ITPUB
Master Shell Scripting: From Basics to Advanced Techniques

Shell Programming

1. Introduction

Shell is a C‑written program that provides a command language and a scripting environment for accessing operating‑system kernel services. A Shell script is a program written for the shell; "shell programming" usually refers to writing these scripts rather than developing the shell itself. Linux systems typically include several shell interpreters; you can list them with cat /etc/shells. Bash is the most common because it is free, easy to use, and the default on most distributions.

2. Quick Start

2.1 Write a script

Create a file /export/hello.sh with the following content:

#!/bin/bash

echo 'hello world'

The #! line tells the system which interpreter to use.

2.2 Execute the script

Method 1 : run the interpreter explicitly

/bin/sh hello.sh
# or
/bin/bash hello.sh

Both produce hello world. In this context sh is a shortcut to bash.

Method 2 : invoke the script directly after giving it execute permission

chmod 755 hello.sh
./hello.sh

3. Shell Variables

3.1 Definition

Assign a value without a leading $ and without spaces around the equals sign:

your_name="runoob.com"

Variable names may contain letters, numbers, and underscores, but cannot start with a number or contain punctuation or Bash reserved keywords.

Valid: RUNOOB, LD_LIBRARY_PATH, _var, var2 Invalid: ?var=123,

user*name=runoob

3.2 Using variables

Reference a variable with $ or ${} for clarity:

your_name="zhangsan"

echo $your_name

echo ${your_name}

3.3 Deleting variables

Use unset to remove a variable (cannot delete read‑only variables):

unset myUrl

3.4 Read‑only variables

Mark a variable as read‑only with readonly. Subsequent assignments generate an error:

myUrl="http://www.google.com"
readonly myUrl
myUrl="http://www.runoob.com"   # error

4. Strings

4.1 Single quotes

skill='java'
str='I am good at $skill'
echo $str   # prints: I am good at $skill

Variables are not expanded inside single quotes.

4.2 Double quotes

skill='java'
str="I am good at $skill"
echo $str   # prints: I am good at java

Double quotes allow variable expansion and escape sequences.

4.3 String length

skill='java'
echo ${#skill}   # 4
expr length "iamlilei"   # 8

4.4 Sub‑string extraction

str="I am good at java"
# from character 2 to the end
echo ${str:2}          # am good at java
# from character 2, length 2
echo ${str:2:2}        # am

4.5 Finding a sub‑string

str="I am good at java"
expr index "$str" am   # 3 (position of first "am")

5. Passing Parameters

When a script is invoked, arguments are accessed as $1, $2, …, $0 (script name). Special variables: $# – number of arguments $* – all arguments as a single string $@ – all arguments as separate quoted strings $$ – PID of the current shell $? – exit status of the last command

Example param.sh:

#!/bin/bash

echo "Shell parameter example!"
echo "Script name: $0"
echo "First param: $1"
echo "Second param: $2"
echo "Third param: $3"

Run with chmod 755 param.sh && ./param.sh 1 2 3 to see the values.

6. Arithmetic Operators

Bash supports arithmetic via expr, $(( )), $[ ], etc. Operators must be spaced. + – addition (e.g., expr $a + $b) - – subtraction * – multiplication (escape * when using expr) / – division % – modulo = – assignment == – numeric equality test != – numeric inequality test

Example:

#!/bin/bash

a=4
b=20
val=`expr $a + $b`
echo $val   # 24
val=`expr $a - $b`
echo $val   # -16
val=`expr $a \* $b`
echo $val   # 80
val=`expr $b / $a`
echo $val   # 5
((c=a+b))
echo $c   # 24

7. Flow Control

7.1 if / else

#!/bin/bash
a=20
if [ $a -gt 10 ]; then
    echo "a is greater than 10"
fi

Extended forms with elif and else allow multiple branches.

7.2 for loops

# iterate 1 to 5
for i in 1 2 3 4 5; do
    echo $i
done

# iterate 1 to 100
for i in {1..100}; do echo $i; done

# iterate odd numbers 1 to 100
for i in {1..100..2}; do echo $i; done

# iterate files in root directory
for f in `ls /`; do echo $f; done

7.3 while loops

#!/bin/bash
sum=0
i=1
while [ $i -le 100 ]; do
    sum=$[ sum + i ]
    i=$[ i + 1 ]
 done
echo $sum   # 5050

7.4 Infinite loop with break

#!/bin/bash
i=0
while true; do
    echo "$i $(date +"%Y-%m-%d %H:%M:%S")"
    i=$[ i + 1 ]
    if [ $i -eq 10 ]; then
        break
    fi
done

7.5 continue

#!/bin/bash
for i in {1..30}; do
    if [ $[ i % 3 ] -eq 0 ]; then
        continue   # skip multiples of 3
    fi
    echo $i
done

7.6 case (switch)

#!/bin/bash
echo 'Enter a number between 1 and 4:'
read aNum
case $aNum in
    1) echo 'You chose 1' ;;
    2) echo 'You chose 2' ;;
    3) echo 'You chose 3' ;;
    4) echo 'You chose 4' ;;
    *) echo 'Invalid input' ;;
esac

8. Functions

8.1 Definition

# simple function
myfunc() {
    echo "This is my first shell function"
}

# call the function
myfunc

8.2 Parameters to functions

#!/bin/bash
funWithParam() {
    echo "First param: $1"
    echo "Second param: $2"
    echo "Tenth param (using braces): ${10}"
    echo "Total number of params: $#"
    echo "All params as a string: $*"
}

funWithParam 1 2 3 4 5 6 7 8 9 34 73

9. Arrays

9.1 Defining arrays

#!/bin/bash
my_array=(A B "C" D)

9.2 Accessing elements

echo "First element: ${my_array[0]}"
echo "Second element: ${my_array[1]}"

9.3 Getting all elements

echo "All elements: ${my_array[*]}"

9.4 Array length

echo "Number of elements: ${#my_array[*]}"

9.5 Traversing arrays

# method 1 – iterate over values
for val in "${my_array[@]}"; do echo $val; done

# method 2 – index based
len=${#my_array[@]}
for ((i=0;i<len;i++)); do echo ${my_array[$i]}; done

10. Sourcing Other Files

External scripts can be included with . filename or source filename, enabling code reuse.

# test1.sh
my_arr=(AA BB CC)

# test2.sh
source ./test1.sh
for var in "${my_arr[@]}"; do echo $var; done
Shell source example
Shell source example

Conclusion

This overview covers the essential concepts of shell scripting, providing a foundation for automating tasks, managing system resources, and building more complex command‑line tools.

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.

LinuxBashfunctionsVariablesArraysShell scriptingControl Flow
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.