Courses/Command Line/Shell Scripting Basics

    Shell Scripting Basics

    Write your first bash scripts with variables, conditionals, loops, and functions.

    What You'll Learn

    • Creating and running bash scripts
    • Variables, user input, and string operations
    • if/elif/else conditionals and comparison operators
    • for/while loops and reusable functions

    Creating a Bash Script

    A bash script is just a text file containing commands. The first line should be a shebang that tells the system which interpreter to use:

    #!/bin/bash
    # This is a comment
    echo "Hello from a script!"
    date
    whoami

    To run it:

    $ chmod +x script.sh    # Make it executable (once)
    $ ./script.sh           # Run it!
    # OR
    $ bash script.sh        # Run without chmod

    Variables

    name="Alice"              # No spaces around =!
    age=30
    echo "Hello, $name! You are $age."
    echo "Home directory: $HOME"
    echo "Current date: $(date +%Y-%m-%d)"

    ⚠️ Common Mistake

    Spaces around = in variable assignment. name = "Alice" fails — bash interprets name as a command. Always write name="Alice" with no spaces.

    Scripts & Conditionals

    Write your first bash script with variables and if/else.

    Try it Yourself »
    JavaScript
    // Shell Scripting — simulated in JavaScript
    console.log("=== Your First Bash Script ===");
    console.log(`
    #!/bin/bash
    # File: greet.sh
    
    name="World"
    echo "Hello, $name!"
    echo "Today is $(date)"
    echo "You are $(whoami)"
    `);
    
    console.log("$ chmod +x greet.sh   (make it executable)");
    console.log("$ ./greet.sh          (run it!)");
    console.log();
    
    console.log("=== Variables ===");
    let name = "Developer";
    let count = 42;
    console.log("name=" + name);
    console.log("count=" + count);
    console.log("Greeti
    ...

    Loops and Functions

    For Loops

    # Loop over a list
    for color in red green blue; do
      echo "Color: $color"
    done
    
    # Loop over files
    for file in *.txt; do
      echo "Processing $file"
      wc -l "$file"
    done
    
    # C-style loop
    for ((i=1; i<=10; i++)); do
      echo "Number: $i"
    done

    Functions

    greet() {
      local name=$1    # $1 = first argument
      echo "Hello, $name!"
    }
    
    greet "Alice"      # Hello, Alice!
    greet "Bob"        # Hello, Bob!

    💡 Pro Tip

    Always use local for variables inside functions to avoid polluting the global scope. It's the same best practice as const/let in JavaScript.

    Loops & Functions

    Practice for/while loops and reusable functions.

    Try it Yourself »
    JavaScript
    // Shell Loops & Functions — simulated
    console.log("=== For Loop ===");
    console.log("Bash: for i in 1 2 3 4 5; do echo $i; done");
    for (let i = 1; i <= 5; i++) {
      console.log("  Iteration: " + i);
    }
    console.log();
    
    console.log("=== Loop Over Files ===");
    console.log("for file in *.txt; do echo $file; done");
    const files = ["notes.txt", "readme.txt", "todo.txt"];
    files.forEach(f => console.log("  Processing: " + f));
    console.log();
    
    console.log("=== While Loop ===");
    let countdown = 5;
    console.l
    ...

    📋 Quick Reference

    ConceptSyntax
    Shebang#!/bin/bash
    Variablename="value"
    Ifif [ cond ]; then ... fi
    For loopfor x in list; do ... done
    Functionfunc() { ... }
    Make executablechmod +x script.sh

    🎉 Lesson Complete!

    You can now automate tasks with bash scripts! Next: managing running processes with ps, kill, and background jobs.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy PolicyTerms of Service