Bash Scripting Fundamentals
Tags: bash, cmd, coding, terminal
Published: 2024-06-23 08:31:09

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v5a2wuj2gfez67oe3q6n.jpeg)
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.
  ```bash
  nano script.sh
  ```
- **Making the script executable**: 
  ```bash
  chmod +x script.sh
  ```
- **Running the script**:
  ```bash
  ./script.sh
  ```

## 2. Basic Commands

### 2.1 Echo
- **Description**: Prints text to the terminal.
  ```bash
  echo "Hello, World!"
  ```

### 2.2 Variables
- **Description**: Storing and using data.
  ```bash
  NAME="John"
  echo $NAME
  ```

### 2.3 Comments
- **Description**: Adding comments to a script for readability.
  ```bash
  # This is a comment
  echo "This is a script"
  ```

## 3. Input and Output

### 3.1 Reading User Input
- **Description**: Taking input from the user.
  ```bash
  read "Enter your name: " NAME
  echo "Hello, $NAME"
  ```

### 3.2 Redirecting Output
- **Description**: Redirecting command output to a file.
  ```bash
  echo "This is a test" > file.txt
  ```

### 3.3 Appending Output
- **Description**: Appending command output to a file.
  ```bash
  echo "This is a test" >> file.txt
  ```

### 3.4 Redirecting Input
- **Description**: Using a file as input to a command.
  ```bash
  wc -l < file.txt
  ```

### 3.5 Piping
- **Description**: Using the output of one command as input to another.
  ```bash
  cat file.txt | grep "test"
  ```

## 4. Control Structures

### 4.1 Conditional Statements
- **Description**: Making decisions in scripts.
  ```bash
  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
  ```bash
  for i in {1..5}; do
    echo "Welcome $i"
  done
  ```

#### 4.2.2 While Loop
  ```bash
  COUNTER=0
  while [ $COUNTER -lt 5 ]; do
    echo "Counter: $COUNTER"
    COUNTER=$((COUNTER + 1))
  done
  ```

#### 4.2.3 Until Loop
  ```bash
  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.
  ```bash
  my_function() {
    echo "Hello from my_function"
  }
  my_function
  ```

## 6. Data Structures

### 6.1 Arrays
- **Description**: Using arrays to store multiple values.
  ```bash
  NAMES=("John" "Jane" "Doe")
  echo "First Name: ${NAMES[0]}"
  ```

### 6.2 Tuples (Simulated with Arrays)
- **Description**: Using arrays to simulate tuples.
  ```bash
  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.
  ```bash
  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.
  ```bash
  A=5
  B=3
  SUM=$((A + B))
  echo "Sum: $SUM"
  ```

### 7.2 Logical Operators
- **Description**: Using logical operators in conditions.
  ```bash
  if [ $A -eq 5 ] && [ $B -eq 3 ]; then
    echo "Both conditions are true."
  fi
  ```

### 7.3 Semantic Operators
- **Description**: Using comparison operators.
  ```bash
  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.
  ```bash
  VAR="123"
  NUM=$((VAR + 0))  # Cast string to number for arithmetic
  echo $NUM
  ```

## 9. API Calls
- **Description**: Making HTTP requests using tools like `curl`.
  ```bash
  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.
  ```bash
  # 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.
  ```bash
  echo "/path/to/script.sh" | at now + 5 minutes
  ```

### 10.3 Background Jobs
- **Description**: Running scripts in the background.
  ```bash
  ./script.sh &
  ```

## 11. Advanced Topics

### 11.1 Using `sed` and `awk`
- **Description**: Text processing with `sed` and `awk`.

#### 11.1.1 `sed`
  ```bash
  sed 's/old/new/g' file.txt
  ```

#### 11.1.2 `awk`
  ```bash
  awk '{print $1}' file.txt
  ```

### 11.2 Traps
- **Description**: Capturing signals.
  ```bash
  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
  bash -x script.sh
  ```

### 12.2 Using `set`
- **Description**: Setting options for debugging.
  ```bash
  set -x  # Enable debugging
  set +x  # Disable debugging
  ```

### 12.3 Error Handling
- **Description**: Handling errors in scripts.
  ```bash
  command || { echo "Command failed"; exit 1; }
  ```

### 12.4 Using `trap` for Cleanup
- **Description**: Cleaning up resources on script exit.
  ```bash
  cleanup() {
    echo "Cleaning up..."
    # perform cleanup here
  }
  trap cleanup EXIT
  ```

### 12.5 Logging
- **Description**: Creating log files for your scripts.
  ```bash
  LOG_FILE="/var/log/script.log"
  echo "Script started" >> $LOG_FILE
  ```

## 13. More on Arrays

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

### 13.2 Associative Arrays
- **Description**: Using associative arrays.
  ```bash
  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.
  ```bash
  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.
  ```bash
  STRING="Hello World"
  echo ${STRING:6:5}  # Outputs "World"
  ```

### 14.2 String Length
- **Description**: Getting the length of a string.
  ```bash
  echo ${#STRING}  # Outputs "11"
  ```

### 14.3 String Replacement
- **Description**: Replacing part of a string.
  ```bash
  echo ${STRING/World/Bash}  # Outputs "Hello Bash"
  ```

## 15. File Operations

### 15.1 File Tests
- **Description**: Testing properties of files.
  ```bash
  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.
  ```bash
  while IFS= read -r line; do
    echo "$line"
  done < file.txt
  ```

### 15.3 Writing to Files
- **Description**: Writing to a file.
  ```bash
  echo "Hello, World!" > file.txt
  ```

### 15.4 Appending to Files
- **Description**: Appending to a file.
  ```bash
  echo "This is a new line" >> file.txt
  ```

## 16. Network Operations

### 16.1 Downloading Files
- **Description**: Using `wget` or `curl` to download files.
  ```bash
  wget http://example.com/file.zip
  curl -O http://example.com/file.zip
  ```

### 16.2 Uploading Files
- **Description**: Using `curl` to upload files.
  ```bash
  curl -F "file=@/path/to/file" http://example.com/upload
  ```

### 16.3 Checking Internet Connection
- **Description**: Pinging a server to check for internet connectivity.
  ```bash
  ping -c 4 google.com
  ```

## 17. Handling Large Data

### 17.1 Sorting and Uniqueness
- **Description**: Sorting data and removing duplicates.
  ```bash
  sort file.txt | uniq > sorted.txt
  ```

### 17.2 Finding Patterns
- **Description**: Using `grep` to find patterns.
  ```bash
  grep "pattern" file.txt
  ```

### 17.3 Splitting Files
- **Description**: Splitting large files into smaller parts.
  ```bash
  split -l 1000 largefile.txt smallfile_
  ```

## 18. Process Management

### 18.1 Checking Running Processes
- **Description**: Using `ps` to list running processes.
  ```bash
  ps aux
  ```

### 18.2 Killing Processes
- **Description**: Using `kill` to terminate processes.
  ```bash
  kill -9 PID
  ```

### 18.3 Monitoring System Resources
- **Description**: Using `top` or `htop` to monitor system resources.
  ```bash
  top
  htop  # Requires installation
  ```

## 19. Packaging and Distribution

### 19.1 Creating Tar Archives
- **Description**: Archiving files using `tar`.
  ```bash
  tar -cvf archive.tar file1 file2 directory/
  ```

### 19.2 Extracting Tar Archives
- **Description**: Extracting files from a tar archive.
  ```bash
  tar -xvf archive.tar
  ```

### 19.3 Compressing Files
- **Description**: Compressing files using `gzip`.
  ```bash
  gzip file.txt
  ```

### 19.4 Decompressing Files
- **Description**: Decompressing files using `gunzip`.
  ```bash
  gunzip file.txt.gz
  ```

This extensive guide covers fundamental and advanced topics in Bash scripting