Skip to main content

Understanding and Running a Shell Script: A Beginner's Guide

Shell scripting is an invaluable skill for automating tasks in Unix-based systems. In this revised article, we'll explore what a shell script is, how to write a simple "Hello World" script, and the essential steps to make a shell script executable and accessible, including checking the $PATH variable and placing the script in a user-specific bin directory.

What is a Shell Script?

A shell script is a text file containing a series of commands executed by the shell, the command-line interpreter in Unix-based operating systems like Linux and macOS. Shell scripts automate repetitive tasks, manage system operations, and are written in the shell scripting language.

Writing a Simple Shell Script

Let's craft a "Hello World" script that prints its namesake message to the terminal.

  1. Creating the Script File: Open a text editor and input the following:

    #!/bin/bash
    echo "Hello World"

    The #!/bin/bash shebang indicates that this script should run in the Bash shell.

  2. Saving the Script: Save this as hello_world.sh. While .sh isn’t mandatory, it’s a common convention.

Making the Shell Script Executable

Before execution, the script must be made executable via the terminal.

  1. Navigate to Your Script: Use the cd command in the terminal to go to the script's location.

  2. Change File Permissions: Apply the chmod command:

    chmod +x hello_world.sh

    This adjusts the file’s permissions, allowing execution.

Running the Shell Script

With the script executable, you can run it from its current directory.

  1. Execute the Script: In the terminal, enter ./hello_world.sh. "Hello World" should appear on the screen.

Checking the $PATH Variable

Before relocating the script for universal access, it’s prudent to check your $PATH variable.

  1. Display $PATH: In the terminal, type echo $PATH. This will display directories where the system looks for executable files.

Placing the Script in Your Personal Bin Directory

To run your script from anywhere, place it in a directory included in your $PATH. A good practice is to use the bin directory in your home folder.

  1. Create a Bin Directory: If it doesn't exist, create a bin directory in your home folder:

    mkdir -p ~/bin
  2. Move the Script: Move your script to this directory:

    mv hello_world.sh ~/bin
  3. Running from Anywhere: After adding ~/bin to your $PATH (if it’s not already there), you can run hello_world.sh from any location in the terminal.

Conclusion

You've now written, made executable, and appropriately positioned your first shell script. Shell scripting is a powerful tool for automating tasks and enhancing system management. As you become more familiar with scripting, you'll discover its vast potential in streamlining your computing tasks. Happy scripting!

What Can You Do Next 🙏😊

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.

YouTube @cloudaffle