Skip to main content

Command Palette

Search for a command to run...

Introduction to Shell Scripting — Automating Linux with Bash (Part 1)

Published
4 min readView as Markdown

In this lecture series, we move into one of the most important concepts in Linux and DevOps:
the shell.

You will learn:

  • What a shell is

  • What Bash is

  • How shell scripts work

  • How to write and execute basic Bash scripts

Most importantly, you will learn how to automate tasks instead of executing commands one by one.


Why Shell Scripting Matters

So far, we’ve been executing Linux commands manually:

  • Creating users

  • Assigning users to groups

  • Creating folders and files

  • Changing permissions

  • Installing software

  • Starting applications

All of these tasks work perfectly when done once.

But what happens when:

  • You need to configure 10 servers the same way?

  • You want to reproduce a server setup later?

  • A teammate needs the same configuration?

  • You want logic, conditions, or checks before running commands?

  • You want to perform bulk actions, like adding many users or renaming hundreds of files?

Running commands manually becomes slow, error-prone, and hard to track.


The Idea: Put Commands into a File

Instead of typing commands one by one, wouldn’t it be better if we could:

  • Write them into a file

  • Execute them all in the correct order

  • Reuse the same setup anywhere

  • Share the configuration with others

That file is called a shell script.

A shell script:

  • Contains a sequence of Linux commands

  • Can include logic and conditions

  • Can be reused, copied, and stored

  • Is typically saved with a .sh extension


What Is a Shell?

On Unix-like systems (Linux and macOS), the shell is a program that:

  • Takes commands from the user

  • Interprets them

  • Translates them into instructions the kernel understands

In simple terms:

The shell is the interface between the user and the operating system kernel.

Because of this, a file that contains shell commands is called a shell script.


Different Shell Implementations

There isn’t just one shell. There are multiple shell programs.

Bourne Shell (sh)

  • Original Unix shell

  • Located at:

      /bin/sh
    
  • Named after its author, Stephen Bourne

  • Still available on all Unix-like systems

Bash (Bourne Again Shell)

  • Improved and feature-rich version of sh

  • Default shell on most modern Linux systems

  • Fully backward-compatible with sh

  • Supports advanced scripting features

Because Bash is now the standard, the terms:

  • shell scripting

  • bash scripting

are often used interchangeably.

Bash is both a command interpreter and a programming language.


Writing Your First Bash Script

Let’s write a simple script to configure a server.

Step 1: Create the Script File

touch setup.sh

Shell scripts typically end with .sh.


How Does Linux Know Which Shell to Use?

Since multiple shells may exist on a system, Linux needs to know:

  • Which interpreter should run the script

This is done using a shebang line.


The Shebang Line

The shebang line must be the first line of the script.

For Bash:

#!/bin/bash

This line tells Linux:

Use the Bash interpreter at /bin/bash to execute this script.

You can verify the Bash binary exists:

ls /bin/bash

Or:

ls /bin | grep bash

Why Is It Called “Shebang”?

The name comes from the first two characters:

  • # → sharp

  • ! → bang

Together: shebang


Choosing the Shell Interpreter

  • #!/bin/bash → Bash-specific features

  • #!/bin/sh → Maximum portability across Unix systems

If Bash is not installed on a system and your script requires it, execution will fail.


Adding Commands to the Script

Open the script in an editor (for example, Vim):

vim setup.sh

Add the following content:

#!/bin/bash
echo "Setup and configure server"

Save and exit.


Executing a Shell Script

Try running the script:

./setup.sh

You’ll see:

permission denied

Why Permission Denied?

By default, newly created files do not have execute permission.

Check permissions:

ls -l setup.sh

You’ll notice there’s no x flag.


Adding Execute Permission

Grant execute permission to the file owner:

sudo chmod u+x setup.sh

Now:

  • The file becomes executable

  • The file color changes in the terminal

  • The execute flag (x) appears


Running the Script Successfully

./setup.sh

Output:

Setup and configure server

Your shell script is now executed just like a program.


Alternative Way to Execute Bash Scripts

You can also explicitly use Bash:

bash setup.sh

This works even without execute permission, but the ./script.sh method is the standard Unix approach.


Summary

In this lecture, you learned:

  • Why shell scripting is essential for automation

  • What a shell is and how it works

  • The difference between sh and bash

  • What a shebang line is and why it matters

  • How to write a basic Bash script

  • How to make a script executable

  • Different ways to execute shell scripts

This is the foundation of automation in Linux and DevOps.

More from this blog

devops

22 posts