Skip to content

Linux Bash Scripting Guide

Mattscreative edited this page Dec 5, 2025 · 2 revisions

Linux Bash Scripting Guide

Complete beginner-friendly guide to Bash scripting on Linux, covering Arch Linux, CachyOS, and other distributions including script creation, variables, loops, conditionals, and automation.


Table of Contents

  1. Understanding Bash Scripting
  2. Creating Scripts
  3. Variables
  4. Conditionals
  5. Loops
  6. Functions
  7. Input/Output
  8. Troubleshooting

Understanding Bash Scripting

What is Bash Scripting?

Bash scripting automates tasks using shell commands.

Uses:

  • Automation: Automate repetitive tasks
  • System administration: Manage systems
  • File processing: Process files in bulk
  • Backup scripts: Create automated backups

Benefits:

  • Time saving: Automate manual tasks
  • Consistency: Same tasks run the same way
  • Error reduction: Fewer manual errors

Creating Scripts

First Script

Create script:

# Create script file
vim myscript.sh

Add:

#!/bin/bash
echo "Hello, World!"

Make executable:

# Make executable
chmod +x myscript.sh

# Run script
./myscript.sh

Shebang

Script header:

#!/bin/bash
# This tells system to use bash

Variables

Basic Variables

Set variables:

#!/bin/bash
NAME="John"
AGE=25

echo "Name: $NAME"
echo "Age: $AGE"

Environment Variables

Use environment variables:

#!/bin/bash
echo "User: $USER"
echo "Home: $HOME"
echo "Path: $PATH"

Command Substitution

Store command output:

#!/bin/bash
DATE=$(date)
FILES=$(ls)

echo "Date: $DATE"
echo "Files: $FILES"

Conditionals

If Statements

Basic if:

#!/bin/bash
if [ "$1" == "hello" ]; then
    echo "Hello!"
fi

If-Else

If-else:

#!/bin/bash
if [ -f "$1" ]; then
    echo "File exists"
else
    echo "File not found"
fi

Case Statements

Case:

#!/bin/bash
case "$1" in
    start)
        echo "Starting..."
        ;;
    stop)
        echo "Stopping..."
        ;;
    *)
        echo "Usage: $0 {start|stop}"
        ;;
esac

Loops

For Loop

For loop:

#!/bin/bash
for i in 1 2 3 4 5; do
    echo "Number: $i"
done

Loop through files:

#!/bin/bash
for file in *.txt; do
    echo "Processing: $file"
done

While Loop

While loop:

#!/bin/bash
count=1
while [ $count -le 5 ]; do
    echo "Count: $count"
    count=$((count + 1))
done

Functions

Define Functions

Create function:

#!/bin/bash
greet() {
    echo "Hello, $1!"
}

greet "John"

Function with Return

Return value:

#!/bin/bash
add() {
    result=$(( $1 + $2 ))
    echo $result
}

sum=$(add 5 3)
echo "Sum: $sum"

Input/Output

Read Input

Get user input:

#!/bin/bash
echo "Enter your name:"
read name
echo "Hello, $name!"

Command Line Arguments

Use arguments:

#!/bin/bash
echo "Script: $0"
echo "First argument: $1"
echo "Second argument: $2"
echo "All arguments: $@"

Redirect Output

Save output:

#!/bin/bash
# Redirect to file
echo "Hello" > output.txt

# Append to file
echo "World" >> output.txt

Troubleshooting

Script Not Running

Check permissions:

# Make executable
chmod +x script.sh

# Check shebang
head -1 script.sh

Debug Script

Debug mode:

# Run with debug
bash -x script.sh

# Check syntax
bash -n script.sh

Summary

This guide covered Bash scripting basics, variables, conditionals, loops, and functions for Arch Linux, CachyOS, and other distributions.


Next Steps


This guide covers Arch Linux, CachyOS, and other Linux distributions. For distribution-specific details, refer to your distribution's documentation.

Clone this wiki locally