Fundamentals 24 min read

Master Shell Scripting: From Variables to Advanced Sed Editing

This comprehensive guide walks you through the essentials of Bash shell scripting, covering variable definitions, printing, arithmetic, control structures, loops, arrays, functions, file I/O, sed stream editing, modular scripts, and interactive menus, all illustrated with clear code examples.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Shell Scripting: From Variables to Advanced Sed Editing

0. Background

The author previously wrote a series of shell practice articles and now provides a concise introduction to shell scripting for beginners, summarizing key concepts learned over time.

1. Syntax

1.1 Variables

Variables are defined without spaces around the equal sign. Example:

str="string"
echo $str
str=123
echo $str

Variables can be referenced with $var or ${var}, and command substitution can assign command output to a variable.

1.2 Printing

Use echo to output strings, variables, or mixed content. Important options: -e enables escape sequences (e.g., \t for tab). -n suppresses the trailing newline.

str4="string4"
echo $str4
echo "str4=$str4"
echo "str4=$str4 str3=$str3"

1.3 Arithmetic

Two common methods:

Using expr (requires escaping *).

Using $[ ... ] which is simpler and does not need escaping.

# expr example
result=$(expr 5 + 5)
echo $result
# $[ ] example
result=$[5 * 5]
echo $result

1.4 Control Structures

Conditional statements use if / else / fi. Numeric comparisons include -eq, -gt, -lt, etc. String comparisons use =, !=, <, >, -n, and -z. File tests include -d, -e, -f, -r, -w, -x, etc.

if [[ 3 -gt 7 ]]; then
echo "yes"
else
echo "no"
fi

1.5 Loops

Supported loops: for (( i=0; i<10; i++ )) – C‑style loop. for item in $(cat file) – iterates over lines.

while [[ condition ]]; do ... done
until [[ condition ]]; do ... done
# C‑style for loop
num=0
for (( i=0; i<10; i++ )); do
  num=$[ $num + $i ]
done
echo "result = $num"

1.6 Arrays (Containers)

Declare indexed arrays with declare -a name. Associative arrays require Bash ≥4.1 ( declare -A name). Example of storing lines in an array and iterating:

declare -a config_content_array
config_content_array[0]="first line"
echo ${config_content_array[0]}

1.7 Functions (Methods)

Define functions using either:

function func1 { ... }
func2() { ... }

Return values can be:

Exit status (0‑255) via return.

Standard output via echo.

Global variables.

function func1 {
  echo "func1 invoked"
  return 280   # actual exit code will be 280 % 256 = 24
}
func2() { echo "return value" }

2. Files

2.1 Reading Files

Read a file line‑by‑line using cat with IFS=$'\n' to preserve line breaks.

file="data"
IFS_OLD=$IFS
IFS=$'
'
for line in $(cat $file); do
  echo "$line"
done
IFS=$IFS_OLD

2.2 Reading Directories

Iterate over directory entries with ls and for, handling sub‑directories recursively.

function read_implement_file_recursively {
  if [[ -d $1 ]]; then
    for item in $(ls $1); do
      itemPath="$1/$item"
      if [[ -d $itemPath ]]; then
        echo "Processing directory $itemPath"
        read_implement_file_recursively $itemPath
      else
        echo "Processing file $itemPath"
      fi
    done
  else
    echo "err: not a directory"
  fi
}
read_implement_file_recursively $(pwd)

2.3 Writing Files

Use output redirection > to overwrite or >> to append. Combine with sort and uniq for sorting and deduplication.

file="subfolder/data2"
destfile="subfolder/data2-p"
sort $file | uniq > $destfile

3. Sed Stream Editing

GNU sed is used for text manipulation (search, replace, insert, delete). Install GNU sed on macOS if needed.

# Check for GNU sed and install via Homebrew if missing
which_sed=$(which sed)
if [[ $(expr "$which_sed" : ".*/gnu-sed/") -gt 0 ]]; then
  echo "GNU sed detected"
else
  brew install gnu-sed --with-default-names
fi

Common sed options: -e add script. -f read script from file. -n suppress automatic printing. -i edit files in‑place.

Common operations: s – substitute. a – append after a line. d – delete a line.

# Append injected content after a method start line
sed -i '/^- \(.*\){$/a\ "${injected_content}"' $file

# Delete lines matching a pattern
sed -i '/${pattern_str}/d' $file

# Replace all occurrences of a class name
sed -i '{ s/${original_class_name}/${result_class_name}/g }' $(grep ${original_class_name} -rl $pbxproj_dir)

4. Modules

4.1 Utility Modules

Separate reusable functions into their own script files and source them.

# Math.sh
power(){
  base=$1
  exp=$2
  result=1
  for (( i=0; i<$exp; i++ )); do
    result=$[ $result * $base ]
  done
  echo $result
}

# Main script
. ./Math.sh
result=$(power 3 5)
echo "3^5 = $result"

4.2 Process Modules

Modules that accept command‑line options via getopts and perform more complex tasks.

# Example of option parsing
while getopts :i:o: opt; do
  case "$opt" in
    i) param_input_dir=$OPTARG;;
    o) param_output_file=$OPTARG;;
    *) echo "Unknown option: $opt";;
  esac
done

5. Input and Menus

5.1 Getting User Input

# Prompt for a directory and validate it
checkInputDestDirRecursive(){
  echo -n "Please enter a directory: "
  read path
  if [[ -d $path ]]; then
    CheckInputDestDirRecursiveReturnValue=$path
  else
    echo -n "Invalid directory, "
    checkInputDestDirRecursive
  fi
}

5.2 Interactive Menu

function genMenu {
  clear
  echo -e "\t\t\tOption Menu
"
  echo -e "\t1. Delete injected content"
  echo -e "\t2. Add injected content"
  echo -e "\t0. Exit menu

"
  echo -en "\t\tEnter option: "
  read -n 1 option
}
while true; do
  genMenu
  case $option in
    0) echo "Bye"; exit 0;;
    1) removeInjectedContent;;
    2) addInjectedContent;;
    *) echo "Wrong!!";;
  esac
  echo
  echo -en "

\tHit any key to continue"
  read -n 1 line
done
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.

BashfunctionsVariablesShell scriptingLoopsfile-handlingsed
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.