basic shell script example

Mastering Shell Scripting: A Basic Example

By Thomas Rodriguez
AD

Welcome to the world of shell scripting! If you're new to the Linux environment, you might be wondering what all the fuss is about. Shell scripts are powerful tools that can automate tasks, simplify complex commands, and even create simple programs. Let's dive into a basic shell script example to get you started.

Before we begin, ensure you have a Linux distribution installed on your computer. For this example, we'll use Bash, the default shell on most Linux systems. If you're using a different shell, the commands might vary slightly.

Understanding Shell Scripts

At its core, a shell script is a plain text file containing a series of commands. These commands are executed in the order they appear, making it easy to automate repetitive tasks. Shell scripts can also include conditional statements and loops, allowing for more complex automation.

AD
How to Create/Write a Simple/Sample Linux Shell/Bash Script
How to Create/Write a Simple/Sample Linux Shell/Bash Script
All PowerShell Operators Explained
All PowerShell Operators Explained
a table that has some different types of words on it, including the names and abbreviations
a table that has some different types of words on it, including the names and abbreviations

To create a shell script, you'll need a text editor. For this example, we'll use nano, a simple and user-friendly text editor. Open your terminal and type:

Creating a Shell Script File

To create a new file named script.sh, type the following command and press Enter:

Shell Scripting Tutorial: How to Create Shell Script in Linux/Unix
Shell Scripting Tutorial: How to Create Shell Script in Linux/Unix
How to Perform Syntax Checking Debugging Mode in Shell Scripts
How to Perform Syntax Checking Debugging Mode in Shell Scripts
Client Challenge
Client Challenge
nano script.sh

This will open a new file in the nano text editor. You can now start writing your shell script.

Writing Your First Shell Script

Let's create a simple script that displays a greeting message. Type the following lines into the file:

AD
#!/bin/bash
echo "Hello, World!"

The first line, known as the shebang (#!/bin/bash), tells the system which interpreter to use to execute the script. The second line uses the echo command to display a message.

Linux Shell Scripting Command Cheat Sheet
Linux Shell Scripting Command Cheat Sheet
Shell Scripts in Termux Made Simple: Automate Your Android Workflow
Shell Scripts in Termux Made Simple: Automate Your Android Workflow
echo: Display Text in Terminal
echo: Display Text in Terminal
How to Send Email using Shell Script | Send mails using a Bash Script | Shell Scripting for DevOps
How to Send Email using Shell Script | Send mails using a Bash Script | Shell Scripting for DevOps
Top 11 Resources to Learn Shell Scripting for Free
Top 11 Resources to Learn Shell Scripting for Free
What Is Shell Scripting and Why You Should Use It
What Is Shell Scripting and Why You Should Use It

Making Your Script Executable

Before you can run your script, you need to give it execute permissions. In your terminal, type:

chmod +x script.sh

This command makes the script executable. Now you can run your script by typing:

AD
./script.sh

You should see the message "Hello, World!" displayed in your terminal.

Bash Scripting Tutorial – Linux Shell Script and Command Line for Beginners | digital daily
Bash Scripting Tutorial – Linux Shell Script and Command Line for Beginners | digital daily
the sea shell font and cursive writing is displayed on a white background with seashells
the sea shell font and cursive writing is displayed on a white background with seashells
Small Shell Script Machine Embroidery Font
Small Shell Script Machine Embroidery Font
Reality Shifting: Scripting Basics
Reality Shifting: Scripting Basics
the back cover of linux cli, which is written in black and yellow on a beige background
the back cover of linux cli, which is written in black and yellow on a beige background
four different types of words that are used to describe the same thing in each language
four different types of words that are used to describe the same thing in each language
Reality Shifting: Scripting Basics
Reality Shifting: Scripting Basics
credit to tania.fontanet on ig!!
credit to tania.fontanet on ig!!
Scripting
Scripting
Linux Command Line and Shell Scripting Bible - (Bible (Wiley)) 5th Edition by Richard Blum (Paperback)
Linux Command Line and Shell Scripting Bible - (Bible (Wiley)) 5th Edition by Richard Blum (Paperback)
Summer Seashell Font
Summer Seashell Font
Seashell Font Free Download
Seashell Font Free Download
the alphabet of seashells
the alphabet of seashells

Adding Arguments to Your Script

Let's enhance our script to greet the user by their name. Modify the script as follows:

#!/bin/bash
echo "Hello, $1!"

The $1 variable represents the first argument passed to the script. Save the file and run it again with an argument, like this:

AD
./script.sh World

You should now see the message "Hello, World!"

That's it! You've just created your first shell script. As you can see, shell scripting is a powerful tool that can simplify your work and automate repetitive tasks. Whether you're a seasoned Linux user or just starting out, understanding shell scripts is an essential skill in the Linux environment.

Now that you have a basic understanding of shell scripts, why not explore more complex scripts and see what you can create? The possibilities are endless!

AD