Shell Scripting for Absolute Beginners | Learn in 3 hours | Start from Zero

4 min read 1 year ago
Published on Aug 03, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial is designed to help absolute beginners learn shell scripting by breaking down the essential concepts and commands needed to write effective shell scripts. Shell scripting is a valuable skill in fields like system administration, DevOps, and automation, making it relevant for anyone looking to streamline their workflows or prepare for technical interviews.

Step 1: Understanding Shell Scripting Basics

  • What is Shell Scripting?
    Shell scripting is the process of automating tasks through a script written for the shell, which is a command-line interface in Unix/Linux systems. It allows you to execute multiple commands in sequence and automate repetitive tasks.

  • Why Use Shell Scripting?
    Automation reduces manual effort, speeds up processes, and minimizes human error. For instance, instead of manually creating 100 files, you can write a script to do it with a single command.

  • Setting Up Your Environment:

    • Use a Linux distribution on your computer or set up a virtual machine.
    • If on Windows, consider using WSL (Windows Subsystem for Linux) or install a virtual machine with Linux.

Step 2: Creating Your First Shell Script

  • Create a New File:
    Use the touch command to create a new script file.

    touch first_script.sh
    
  • Open the File in a Text Editor:
    Use vim or nano to edit the file.

    vim first_script.sh
    
  • Add Shebang Line:
    The first line of your script should specify the interpreter.

    #!/bin/bash
    
  • Write a Simple Command:
    Add a command to your script to print text.

    echo "Hello, World!"
    
  • Save and Exit the Editor:
    In vim, press Esc, then type :wq to save and quit.

  • Make the Script Executable:
    Use chmod to change the file permissions.

    chmod +x first_script.sh
    
  • Run Your Script:
    Execute your script to see the output.

    ./first_script.sh
    

Step 3: Using Common Shell Commands

  • Listing Files:
    Use ls to list files in the current directory.

    ls -l
    
  • Getting Help:
    Use the man command followed by any command to get its manual.

    man ls
    
  • Creating Files:
    Use the touch command to create empty files or use echo to create files with content.

    echo "Content" > file.txt
    
  • Viewing File Contents:
    Use the cat command to view file contents.

    cat file.txt
    

Step 4: Writing Conditional Statements and Loops

  • Using If Statements:

    if [ $a -gt $b ]; then
        echo "$a is greater than $b"
    else
        echo "$a is not greater than $b"
    fi
    
  • Using For Loops:

    for i in {1..10}; do
        echo "Number: $i"
    done
    

Step 5: Advanced Commands and Techniques

  • Using grep to Filter Text:
    Filter output based on specific patterns.

    echo "Hello World" | grep "Hello"
    
  • Using curl to Fetch Remote Files:
    Download content from the web or access APIs.

    curl http://example.com/file.txt
    
  • Using find to Search for Files:

    find /path/to/search -name "*.txt"
    

Step 6: Automating Tasks with Cron Jobs

  • Setting Up a Cron Job:
    • Open the crontab editor.
      crontab -e
      
    • Add a line to schedule a script to run at specific intervals.
      0 6 * * * /path/to/script.sh
      
    This runs script.sh every day at 6 AM.

Conclusion

By following this tutorial, you have learned the fundamentals of shell scripting, including how to create scripts, use common commands, and implement conditional logic and loops. You can further enhance your skills by practicing with more complex scripts and exploring shell scripting in real-world applications. As a next step, consider exploring advanced topics like error handling, debugging scripts, and using external libraries or tools to expand your capabilities in shell scripting.