Introduction to Bash Scripting

By Anurag Singh

Updated on Dec 17, 2024

Introduction to Bash Scripting

In this tutorial, we'll give an introduction to Bash Scripting.

Bash scripting is one of the most powerful and essential tools for system administrators, developers, and Linux enthusiasts. Whether you're automating tasks, managing system processes, or building powerful workflows, understanding Bash scripting can significantly enhance your productivity and control over a Linux environment.

We will introduce you to Bash scripting, explain its benefits, and guide you through setting up your environment to get started. By the end, you will be ready to write your first script and explore the foundational concepts of Bash.

Prerequisites

Before getting started, ensure you have the following:

  • A KVM VPS or dedicated server with any Linux distro installed.
  • A non-root user with sudo privileges.
  • Basic knowledge of Linux commands.

What is Bash Scripting?

Bash (Bourne Again Shell) is a Unix shell and command-line interpreter that executes commands entered by the user. It is also a scripting language that allows you to automate commands, perform repetitive tasks, and write programs to handle complex processes.

A Bash script is essentially a series of commands written in a file. When executed, the shell reads the file and runs the commands sequentially. Scripts can be used to:

  • Automate system administration tasks (backups, updates, monitoring)
  • Process data and files (e.g., renaming files, generating reports)
  • Build custom workflows and pipelines
  • Manage servers and applications
  • Combine multiple commands into reusable scripts

Bash scripting is especially useful because Bash is pre-installed on most Unix-based operating systems, including Linux and macOS.

Benefits and Use Cases of Bash Scripting

1. Automation

Manual tasks like updating software, cleaning up log files, or performing backups can be automated with Bash scripts. Automation saves time, reduces human error, and increases efficiency.

2. Task Scheduling

Bash scripts can be scheduled to run at specific times using tools like cron. This is useful for routine tasks like database backups or system maintenance.

3. System Administration

System administrators frequently use Bash scripts to monitor system health, configure environments, and manage user accounts.

4. File Management

Bash scripts can automate file operations such as renaming, moving, copying, compressing, or deleting large numbers of files.

5. Custom Pipelines

Bash allows chaining commands together using pipes (|) to process data efficiently. Scripts can handle input/output streams, parse logs, and analyze data.

6. Rapid Prototyping

Developers use Bash to quickly prototype solutions without requiring complex tools or frameworks.

7. Portability

Bash scripts are lightweight, portable, and run on almost all Unix-based systems without modification.

By mastering Bash scripting, you can achieve a higher level of control over Linux systems, streamline processes, and gain a deeper understanding of the command line.

Setting Up Your Environment

Before you start writing Bash scripts, ensure you have a suitable environment set up. Follow these steps:

Step 1: Check for Bash Installation

Most Linux distributions and macOS come with Bash pre-installed. To confirm Bash is available on your system, open a terminal and type:

bash --version

The output should display the version of Bash installed. For example:

GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)

If Bash is not installed, you can install it using your package manager:

On Debian/Ubuntu-based systems:

sudo apt update
sudo apt install bash

On RHEL/CentOS-based systems:

sudo yum install bash

On macOS (if needed):

Bash can be installed via Homebrew:

brew install bash

Step 2: Create a Working Directory

Create a directory where you will store your Bash scripts. Organizing your scripts makes it easier to manage projects and access files.

mkdir ~/bash-scripts
cd ~/bash-scripts

Step 3: Set File Permissions

To execute a script, you need to set the correct permissions. By default, newly created files are not executable. Use the chmod command to make a script executable:

chmod +x script_name.sh

We'll explore this in detail when writing your first script.

Step 4: Text Editor

A text editor is required to write Bash scripts. Use any editor of your choice, such as:

Nano: Lightweight, terminal-based editor

nano script_name.sh

Vim/Vi: Powerful terminal-based editor

vim script_name.sh

Visual Studio Code: A modern code editor with extensions for Bash scripting

code script_name.sh

Ensure you save the script with a .sh extension to indicate it is a shell script.

Step 5: Verify the Default Shell

Although Bash is widely used, some systems may use a different default shell (e.g., sh or zsh). To ensure your script runs with Bash, specify the interpreter at the top of your script. This is done using a shebang (#!) line:

#!/bin/bash

The #!/bin/bash line tells the system to use Bash as the interpreter for the script.

Writing Your First Bash Script

Now that your environment is ready, let’s create a simple Bash script.

Open your terminal and navigate to your working directory:

cd ~/bash-scripts

Create a new script file using your preferred text editor:

nano hello_world.sh

Add the following code:

#!/bin/bash
# This is a simple Bash script

echo "Hello, World!"

The first line #!/bin/bash specifies Bash as the interpreter.

The echo command prints the text to the terminal.

Save and exit the editor:

For Nano: Press Ctrl + O (write) and Ctrl + X (exit).

Make the script executable:

chmod +x hello_world.sh

Run the script:

./hello_world.sh

You should see the output:

Hello, World!

Understanding the Script

  • The shebang (#!/bin/bash) ensures the script runs using Bash.
  • The echo command outputs the string Hello, World! to the terminal
  • chmod +x makes the script executable.
  • The script is executed with ./script_name.sh.

Congratulations! You've written and executed your first Bash script. This simple example introduces the fundamental structure of a script, and you can now build on this knowledge to create more advanced automation workflows.

Conclusion

Bash scripting is an essential skill for Linux users, enabling you to automate tasks, manage systems efficiently, and save time on repetitive operations. In this introduction, you learned what Bash scripting is, its benefits, and how to set up your environment to begin writing scripts.

As you progress through this learning series, you'll explore key Bash concepts such as variables, loops, conditionals, functions, and more. By mastering these, you'll unlock the full potential of Bash and streamline your workflows.

Stay tuned for the next tutorial, where we’ll dive deeper into variables and user input in Bash scripts!