Forem

Harsh Pandhe
Harsh Pandhe

Posted on

Day 04: Shell Scripting Basics

Welcome to Day 4 of our 5-day Linux series! Today, we’re diving into the magical world of Shell Scripting, where you’ll learn how to automate repetitive tasks, write simple scripts, and look like a wizard at the command line. Let’s get scripting!
Wizard


What is Shell Scripting?

A shell script is like a to-do list for your computer, written in a language your Linux shell understands (like Bash). Instead of typing commands one by one, you can write a script to do it all for you in one go. It’s perfect for:

  • Automating tasks like backups and updates.
  • Managing files and directories.
  • Creating tools to simplify your workflow.

Why Learn Shell Scripting?

Shell

  • Saves Time: Automate boring, repetitive tasks.
  • Boosts Productivity: Handle complex operations with ease.
  • It’s Fun: Who doesn’t love feeling like a tech wizard?

Getting Started with Shell Scripting

Let’s create your first shell script. Follow these steps:

Step 1: Create a Script File

Open your terminal and create a new file:

nano myscript.sh
Enter fullscreen mode Exit fullscreen mode

Step 2: Add the Shebang Line

The first line of your script should be:

#!/bin/bash
Enter fullscreen mode Exit fullscreen mode

This tells Linux to use the Bash shell to interpret the script.

Hello

Step 3: Write Your First Script

Add the following code to your file:

#!/bin/bash

echo "Hello, World!"
Enter fullscreen mode Exit fullscreen mode

hello world

This script will simply print “Hello, World!” to the terminal.

Step 4: Save and Exit

Press Ctrl + O, then Enter to save. Press Ctrl + X to exit.

Step 5: Make the Script Executable

Run this command to make your script executable:

chmod +x myscript.sh
Enter fullscreen mode Exit fullscreen mode

Step 6: Run Your Script

Finally, execute your script with:

./myscript.sh
Enter fullscreen mode Exit fullscreen mode

Boom! Your first shell script just ran. High five!


Key Concepts in Shell Scripting

Variables

Variables store data that you can use later in your script. Example:

#!/bin/bash

name="Linux User"
echo "Hello, $name!"
Enter fullscreen mode Exit fullscreen mode

Conditionals

Run different commands based on conditions. Example:

#!/bin/bash

if [ $1 -gt 10 ]; then
  echo "The number is greater than 10."
else
  echo "The number is 10 or less."
fi
Enter fullscreen mode Exit fullscreen mode

(Note: $1 refers to the first argument passed to the script.)
if

Loops

Repeat commands using loops. Example:

#!/bin/bash

for i in {1..5}; do
  echo "Number: $i"
done
Enter fullscreen mode Exit fullscreen mode

Example: Automating a Backup

Let’s write a script to back up a directory:

backup.sh

#!/bin/bash

src="/path/to/source"
dest="/path/to/backup"

echo "Starting backup..."
cp -r $src $dest
echo "Backup completed!"
Enter fullscreen mode Exit fullscreen mode
  1. Replace /path/to/source with the directory you want to back up.
  2. Replace /path/to/backup with your backup destination.
  3. Run the script, and boom—automated backup!

Best Practices for Shell Scripting

  • Add Comments: Use # to explain your code.
  • Handle Errors: Use set -e to stop the script on errors.
  • Test Your Scripts: Run them in a safe environment first.
  • Keep It Simple: Avoid making scripts overly complex.

Resources to Learn More


Conclusion

You just took your first steps into the exciting world of shell scripting! With practice, you’ll be automating tasks and building your own tools in no time. Keep experimenting and having fun.

What’s Next?

Tomorrow, we’ll wrap up our Linux series with System Administration, where we’ll learn how to manage users, processes, and services like a pro. See you on Day 5!

Top comments (0)