Introduction to Shell Scripting: A Beginner’s Guide

RB Punna - Oct 9 - - Dev Community

Shell scripting is a powerful way to automate tasks in Linux environments. If you find yourself typing the same commands repeatedly, shell scripts allow you to bundle these commands into a single executable file. This is particularly useful in DevOps, system administration, and development, where automation is key.

In this guide, we’ll cover the basics of shell scripting, from creating your first script to executing and managing file permissions.

Getting Started with Shell Scripting

1. Creating a Shell Script
To create a shell script, you need to start by creating an empty file. You can use the touch command to create a new file:

touch test.sh
Enter fullscreen mode Exit fullscreen mode

The .sh extension is optional but recommended to denote that this file is a shell script.

2. Listing Files and Understanding Manual Pages
After creating your script, you can list the files in the current directory using the ls command:

ls
Enter fullscreen mode Exit fullscreen mode

To understand the various options for a command, use the man (manual) command. For example, to see the options for ls:

man ls
Enter fullscreen mode Exit fullscreen mode

3. Opening and Editing Shell Scripts
To start writing in your shell script, open the file using a text editor like vi or vim:

Open an existing file or create a new one:

vi test.sh
Enter fullscreen mode Exit fullscreen mode

or

vim test2.sh
Enter fullscreen mode Exit fullscreen mode

Once inside the editor, press i to switch to insert mode and start typing.

The First Line of Every Shell Script: Shebang (#!/bin/bash)
Every shell script should begin with a shebang. This is a special line that tells the system which interpreter to use to execute the script. The most common shebang for Bash scripts is:

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

This tells the system to use the Bash shell to run the script. Note that #!/bin/sh was commonly used, but as some systems have changed the default shell to dash, it’s safer to explicitly use /bin/bash for compatibility.

Writing Your First Shell Script

Let’s start with something simple: printing text to the screen. Use the echo command in your script to display a message:

#!/bin/bash
echo "My first shell script"
Enter fullscreen mode Exit fullscreen mode

After saving this file (:wq in vi/vim), you need to make the script executable.

Making a Shell Script Executable

By default, newly created scripts aren’t executable. You can grant execute permissions using the chmod command:

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

Now, you can run your script:

./test.sh
Enter fullscreen mode Exit fullscreen mode

Alternatively, you can also execute the script using sh:

sh test.sh
Enter fullscreen mode Exit fullscreen mode

Working with File Permissions

Linux uses a permission model that includes read (r), write (w), and execute (x) permissions for the file owner, group, and others. Here’s how to modify permissions for your shell scripts:

-
Give all permissions** (read, write, and execute) to everyone:

chmod 777 firstfile.sh
Enter fullscreen mode Exit fullscreen mode

This grants full control to the user, group, and others.

  • Read-only permissions:

chmod 444 firstfile.sh
This allows users to read the file but not modify or execute it.

Understanding Numeric Permissions

The numeric permissions in chmod correspond to:

4 = Read
2 = Write
1 = Execute

So, chmod 777 gives read, write, and execute permissions to everyone, while chmod 444 gives only read permissions.

Writing a More Complex Shell Script

Let’s create a slightly more complex shell script that creates a directory and two files inside it:

#!/bin/bash

# Create a directory
mkdir myfolder

# Move into the new directory
cd myfolder

# Create two files
touch file1.txt file2.txt
Enter fullscreen mode Exit fullscreen mode

Save and exit the file, then give it execute permissions:

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

Run the script, and you’ll see that it creates the folder and files:

./firstbash.sh
Enter fullscreen mode Exit fullscreen mode

Essential Commands in Shell Scripting

Here are some other important commands you can use in shell scripting:

Disk Space Information: Use df to check disk space usage.

df -h
Enter fullscreen mode Exit fullscreen mode

CPU Information: Use nproc to display the number of processing units available.

nproc
Enter fullscreen mode Exit fullscreen mode

Memory Information: Use free to check memory usage.

free -g
Enter fullscreen mode Exit fullscreen mode

System Monitoring: Use top to display active processes and resource usage.

top
Enter fullscreen mode Exit fullscreen mode

Stopping a Script: Press Ctrl + C to stop a running script or process.

The Role of Shell Scripts in DevOps

In DevOps, shell scripting plays a critical role in automating repetitive tasks such as:

  • Deploying applications
  • Managing infrastructure
  • Monitoring system resources
  • Performing backups

Automating these tasks reduces human error and ensures consistency in deployments, making shell scripts an essential tool for DevOps professionals.

Using Comments in Shell Scripts

When writing longer scripts, it’s essential to add comments to explain different sections of your script. In Bash, comments begin with the # symbol:

#!/bin/bash

# This is a comment
echo "This script demonstrates shell scripting"
Enter fullscreen mode Exit fullscreen mode

Comments are ignored during execution but help others (or future you) understand your code better.

Conclusion

Shell scripting is a powerful way to automate and streamline tasks in a Linux environment. By learning the basics of scripting, from creating files to managing permissions, you’ll be able to simplify complex processes and boost your productivity.

With commands like chmod, echo, and basic script structures, you're well on your way to mastering shell scripting. And as you grow in your Linux journey, you’ll find countless opportunities to use scripts to solve real-world problems in system administration, development, and DevOps.

Now, it’s your turn: write your first shell script, experiment with the commands, and start automating your tasks today!

. .
Terabox Video Player