Introduction to Shell Scripting: A Beginner’s Guide

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, …


This content originally appeared on DEV Community and was authored by RB Punna

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

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

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

man ls

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

or

vim test2.sh

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

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"

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

Now, you can run your script:

./test.sh

Alternatively, you can also execute the script using sh:

sh test.sh

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

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

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

chmod +x firstbash.sh

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

./firstbash.sh

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

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

nproc

Memory Information: Use free to check memory usage.

free -g

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

top

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"

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!


This content originally appeared on DEV Community and was authored by RB Punna


Print Share Comment Cite Upload Translate Updates
APA

RB Punna | Sciencx (2024-10-09T12:54:01+00:00) Introduction to Shell Scripting: A Beginner’s Guide. Retrieved from https://www.scien.cx/2024/10/09/introduction-to-shell-scripting-a-beginners-guide/

MLA
" » Introduction to Shell Scripting: A Beginner’s Guide." RB Punna | Sciencx - Wednesday October 9, 2024, https://www.scien.cx/2024/10/09/introduction-to-shell-scripting-a-beginners-guide/
HARVARD
RB Punna | Sciencx Wednesday October 9, 2024 » Introduction to Shell Scripting: A Beginner’s Guide., viewed ,<https://www.scien.cx/2024/10/09/introduction-to-shell-scripting-a-beginners-guide/>
VANCOUVER
RB Punna | Sciencx - » Introduction to Shell Scripting: A Beginner’s Guide. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/09/introduction-to-shell-scripting-a-beginners-guide/
CHICAGO
" » Introduction to Shell Scripting: A Beginner’s Guide." RB Punna | Sciencx - Accessed . https://www.scien.cx/2024/10/09/introduction-to-shell-scripting-a-beginners-guide/
IEEE
" » Introduction to Shell Scripting: A Beginner’s Guide." RB Punna | Sciencx [Online]. Available: https://www.scien.cx/2024/10/09/introduction-to-shell-scripting-a-beginners-guide/. [Accessed: ]
rf:citation
» Introduction to Shell Scripting: A Beginner’s Guide | RB Punna | Sciencx | https://www.scien.cx/2024/10/09/introduction-to-shell-scripting-a-beginners-guide/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.