Shell Scripting 101: A Beginner’s Guide to Automating Tasks in the Terminal

Unlock the power of automation and simplify your life by learning the basics of shell scripting in this beginner’s guide.

Photo by Gabriel Heinzer on Unsplash

Welcome to the world of shell scripting!

If you’ve found your way here, it means you’re interested in automating tasks on your computer, or perhaps you want to get your hands dirty in programming but don’t know where to start. Either way, you’ve come to the right place.

I’m going to take you through the basics of shell scripting, one step at a time so that by the end of this article, you’ll be armed with enough knowledge to write your own simple scripts.

Let’s get started!

What is a Shell Script?

Before diving in, let’s talk about what a shell script actually is.

A shell is a program that allows you to interact with your operating system by executing commands. When you open a terminal window, you’re interacting with a shell.

A shell script, then, is just a series of commands saved in a text file that can be run automatically by the shell. The magic here is that you can automate a lot of tasks that would otherwise require manual input.

Why Learn Shell Scripting?

You may be wondering why you should even bother learning shell scripting when there are so many other programming languages out there. Here are a few reasons:

  1. Automation: You can automate repetitive tasks.
  2. Portability: Shell scripts can often be run on any system with a compatible shell.
  3. Quick & Dirty Solutions: Sometimes you don’t need a full-fledged program; a simple script will do.
  4. Learning Curve: Shell scripting is a great entry point to programming in general.
  5. System Administration and Monitoring: Sysadmins can write shell scripts to monitor system resources like CPU usage, disk space, and memory usage

Choosing a Shell

There are multiple types of shells, like Bash (Bourne Again Shell), Zsh (Z Shell), and Fish (Friendly Interactive Shell), among others.

In this article, I’ll focus on Bash, as it is one of the most commonly used shells. If you’re using Linux or macOS, Bash is usually installed by default.

Writing Your First Script

Enough talking. Let’s write some code!

Open up a text editor like nano or vim in the terminal, or even a GUI-based editor, and type the following:


# This is a comment
echo "Hello, World!"

Save the file with a .sh extension, like

To run the script, you’ll first need to give it executable permissions. Open your terminal, navigate to the folder where you saved your script, and run:

chmod +x

Now you can execute your script.


If everything goes well, you should see “Hello, World!” printed on your terminal. Congratulations, you just wrote and ran your first shell script!

Breaking Down the Code

  • #!/bin/bash: This line is called a "shebang." It tells the system to execute the script using Bash.
  • # This is a comment: Any text following a # symbol is treated as a comment and ignored when the script runs.
  • echo "Hello, World!": echo is a command that prints its arguments to the terminal. Here, it prints "Hello, World!"

Variables and User Input

Variables allow you to store and manipulate data. To declare a variable, simply do.

my_variable="Hello again!"

To use it later, prepend the variable name with $:

echo $my_variable  # Outputs: Hello again!

What about getting input from a user? Easy. Use the read command:

echo "What's your name?"
read name
echo "Hello, $name!"

Control Flow: If-Else and Loops

You can introduce logic into your scripts using if-else statements and loops. Here's a simple example using if-else:

if [ $name == "Alice" ]; then
  echo "Welcome, Alice!"
  echo "You're not Alice."

And a basic for loop:

for i in {1..5}; do
  echo "This is iteration number $i"

Making Your Scripts More Robust

It’s always good practice to add error-checking and helpful messages to your scripts. Use if statements to check the exit status of a command, and echo to print helpful messages.

if mkdir new_folder; then
  echo "Folder successfully created."
  echo "Failed to create folder."

Arithmetic Operations

You can perform basic arithmetic operations easily using a few different methods. One of them is using let keyword.

let "sum = 5 + 2"
echo $sum  # Outputs: 7

While Loop

while loops are another control structure you'll often use. Here's a simple script that counts from 1 to 5.

while [ $count -le 5 ]; do
  echo "This is loop iteration $count"
  let "count++"

Passing Arguments

Often, you’ll want to pass arguments to your script when you run it. You can access these inside your script using $1, $2, $3, ...

echo "The first argument is: $1"
echo "The second argument is: $2"


You can also define functions in your script. Functions help you organize code and make it reusable. Here’s how.


my_function() {
  echo "Hello from inside the function!"

# Calling the function

Case Statement

case statements are another way to add logic to your scripts. They're somewhat similar to a series of if-else statements but can be easier to read.


read -p "Do you like coffee? (yes/no): " answer

case $answer in
    echo "Here's a cup of coffee for you!"
    echo "Would you prefer tea, then?"
    echo "I don't understand."

Bonus Example: Reading from a File

Reading from a file line by line is a common task. Here’s how you can do it in Bash.


while read line; do
  echo "Reading line: $line"
done < "my_file.txt"

Just create a my_file.txt in the same directory and add some lines to it. Run the script, and you'll see it reads the file line by line.

Wrapping Up

So, there you have it: a crash course in shell scripting for beginners. We covered what shell scripts are, and why they’re useful, and went through the basics of writing a script.

From declaring variables to control flow, you’re now equipped with the foundational knowledge you need to start automating tasks.

Remember, the best way to learn is by doing. So go ahead, write some scripts, break them, and then fix them. And if you have questions, let me know in the comments. Happy scripting!