Bash Scripting Fundamentals



This content originally appeared on DEV Community and was authored by Zeshan

1. Introduction to Bash

1.1 What is Bash?

Bash (Bourne Again Shell) is a command processor that typically runs in a text window where the user types commands that cause actions.

Image description
Bash can also read and execute commands from a file, called a script.

1.2 Creating and Running a Script

  • Creating a script: Use a text editor to create a file with the .sh extension.
  nano script.sh
  • Making the script executable:
  chmod +x script.sh
  • Running the script:
  ./script.sh

2. Basic Commands

2.1 Echo

  • Description: Prints text to the terminal.
  echo "Hello, World!"

2.2 Variables

  • Description: Storing and using data.
  NAME="John"
  echo $NAME

2.3 Comments

  • Description: Adding comments to a script for readability.
  # This is a comment
  echo "This is a script"

3. Input and Output

3.1 Reading User Input

  • Description: Taking input from the user.
  read -p "Enter your name: " NAME
  echo "Hello, $NAME"

3.2 Redirecting Output

  • Description: Redirecting command output to a file.
  echo "This is a test" > file.txt

3.3 Appending Output

  • Description: Appending command output to a file.
  echo "This is a test" >> file.txt

3.4 Redirecting Input

  • Description: Using a file as input to a command.
  wc -l < file.txt

3.5 Piping

  • Description: Using the output of one command as input to another.
  cat file.txt | grep "test"

4. Control Structures

4.1 Conditional Statements

  • Description: Making decisions in scripts.
  if [ "$NAME" == "John" ]; then
    echo "Hello, John!"
  else
    echo "You are not John."
  fi

4.2 Loops

  • Description: Repeating a set of commands.

4.2.1 For Loop

  for i in {1..5}; do
    echo "Welcome $i"
  done

4.2.2 While Loop

  COUNTER=0
  while [ $COUNTER -lt 5 ]; do
    echo "Counter: $COUNTER"
    COUNTER=$((COUNTER + 1))
  done

4.2.3 Until Loop

  COUNTER=0
  until [ $COUNTER -ge 5 ]; do
    echo "Counter: $COUNTER"
    COUNTER=$((COUNTER + 1))
  done

5. Functions

5.1 Defining and Calling Functions

  • Description: Grouping commands into a reusable block.
  my_function() {
    echo "Hello from my_function"
  }
  my_function

6. Data Structures

6.1 Arrays

  • Description: Using arrays to store multiple values.
  NAMES=("John" "Jane" "Doe")
  echo "First Name: ${NAMES[0]}"

6.2 Tuples (Simulated with Arrays)

  • Description: Using arrays to simulate tuples.
  TUPLE=("Alice" 25 "Developer")
  echo "Name: ${TUPLE[0]}, Age: ${TUPLE[1]}, Job: ${TUPLE[2]}"

6.3 Sets (Simulated with Arrays and Associative Arrays)

  • Description: Using arrays and associative arrays to simulate sets.
  declare -A SET
  SET["Apple"]=1
  SET["Banana"]=1
  if [[ ${SET["Apple"]} ]]; then
    echo "Apple is in the set."
  fi

7. Operators

7.1 Mathematical Operators

  • Description: Performing arithmetic operations.
  A=5
  B=3
  SUM=$((A + B))
  echo "Sum: $SUM"

7.2 Logical Operators

  • Description: Using logical operators in conditions.
  if [ $A -eq 5 ] && [ $B -eq 3 ]; then
    echo "Both conditions are true."
  fi

7.3 Semantic Operators

  • Description: Using comparison operators.
  if [ "$A" -eq 5 ]; then
    echo "A is 5"
  fi
  if [ "$A" -ne 5 ]; then
    echo "A is not 5"
  fi

8. Type Casting

  • Description: Type casting in Bash is generally about ensuring variable content is treated correctly.
  VAR="123"
  NUM=$((VAR + 0))  # Cast string to number for arithmetic
  echo $NUM

9. API Calls

  • Description: Making HTTP requests using tools like curl.
  RESPONSE=$(curl -s -X GET "https://api.example.com/data")
  echo "Response: $RESPONSE"

10. Automating Tasks

10.1 Cron Jobs

  • Description: Scheduling scripts to run automatically.
  # Edit the crontab file
  crontab -e
  # Add a job to run every day at midnight
  0 0 * * * /path/to/script.sh

10.2 Using at

  • Description: Scheduling a one-time task.
  echo "/path/to/script.sh" | at now + 5 minutes

10.3 Background Jobs

  • Description: Running scripts in the background.
  ./script.sh &

11. Advanced Topics

11.1 Using sed and awk

  • Description: Text processing with sed and awk.

11.1.1 sed

  sed 's/old/new/g' file.txt

11.1.2 awk

  awk '{print $1}' file.txt

11.2 Traps

  • Description: Capturing signals.
  trap "echo 'Script interrupted'; exit" SIGINT
  while true; do
    echo "Running..."
    sleep 1
  done

12. Debugging

12.1 Enabling Debugging

  • Description: Running a script in debug mode.
  bash -x script.sh

12.2 Using set

  • Description: Setting options for debugging.
  set -x  # Enable debugging
  set +x  # Disable debugging

12.3 Error Handling

  • Description: Handling errors in scripts.
  command || { echo "Command failed"; exit 1; }

12.4 Using trap for Cleanup

  • Description: Cleaning up resources on script exit.
  cleanup() {
    echo "Cleaning up..."
    # perform cleanup here
  }
  trap cleanup EXIT

12.5 Logging

  • Description: Creating log files for your scripts.
  LOG_FILE="/var/log/script.log"
  echo "Script started" >> $LOG_FILE

13. More on Arrays

13.1 Array Operations

  • Description: Array length and iteration.
  ARR=("one" "two" "three")
  echo "Array length: ${#ARR[@]}"
  for ITEM in "${ARR[@]}"; do
    echo $ITEM
  done

13.2 Associative Arrays

  • Description: Using associative arrays.
  declare -A ASSOC_ARRAY
  ASSOC_ARRAY["name"]="John"
  ASSOC_ARRAY["age"]="30"
  echo "Name: ${ASSOC_ARRAY["name"]}"
  echo "Age: ${ASSOC_ARRAY["age"]}"

13.3 Multidimensional Arrays

  • Description: Simulating multidimensional arrays.
  ARRAY_2D[0]="1 2 3"
  ARRAY_2D[1]="4 5 6"
  ARRAY_2D[2]="

7 8 9"
  for ROW in "${ARRAY_2D[@]}"; do
    for ITEM in $ROW; do
      echo -n "$ITEM "
    done
    echo
  done

14. String Operations

14.1 Substring Extraction

  • Description: Extracting a substring from a string.
  STRING="Hello World"
  echo ${STRING:6:5}  # Outputs "World"

14.2 String Length

  • Description: Getting the length of a string.
  echo ${#STRING}  # Outputs "11"

14.3 String Replacement

  • Description: Replacing part of a string.
  echo ${STRING/World/Bash}  # Outputs "Hello Bash"

15. File Operations

15.1 File Tests

  • Description: Testing properties of files.
  if [ -f "file.txt" ]; then
    echo "file.txt is a regular file."
  fi
  if [ -d "directory" ]; then
    echo "directory is a directory."
  fi

15.2 Reading Files

  • Description: Reading a file line by line.
  while IFS= read -r line; do
    echo "$line"
  done < file.txt

15.3 Writing to Files

  • Description: Writing to a file.
  echo "Hello, World!" > file.txt

15.4 Appending to Files

  • Description: Appending to a file.
  echo "This is a new line" >> file.txt

16. Network Operations

16.1 Downloading Files

  • Description: Using wget or curl to download files.
  wget http://example.com/file.zip
  curl -O http://example.com/file.zip

16.2 Uploading Files

  • Description: Using curl to upload files.
  curl -F "file=@/path/to/file" http://example.com/upload

16.3 Checking Internet Connection

  • Description: Pinging a server to check for internet connectivity.
  ping -c 4 google.com

17. Handling Large Data

17.1 Sorting and Uniqueness

  • Description: Sorting data and removing duplicates.
  sort file.txt | uniq > sorted.txt

17.2 Finding Patterns

  • Description: Using grep to find patterns.
  grep "pattern" file.txt

17.3 Splitting Files

  • Description: Splitting large files into smaller parts.
  split -l 1000 largefile.txt smallfile_

18. Process Management

18.1 Checking Running Processes

  • Description: Using ps to list running processes.
  ps aux

18.2 Killing Processes

  • Description: Using kill to terminate processes.
  kill -9 PID

18.3 Monitoring System Resources

  • Description: Using top or htop to monitor system resources.
  top
  htop  # Requires installation

19. Packaging and Distribution

19.1 Creating Tar Archives

  • Description: Archiving files using tar.
  tar -cvf archive.tar file1 file2 directory/

19.2 Extracting Tar Archives

  • Description: Extracting files from a tar archive.
  tar -xvf archive.tar

19.3 Compressing Files

  • Description: Compressing files using gzip.
  gzip file.txt

19.4 Decompressing Files

  • Description: Decompressing files using gunzip.
  gunzip file.txt.gz

This extensive guide covers fundamental and advanced topics in Bash scripting


This content originally appeared on DEV Community and was authored by Zeshan