What is a Bash Script?

Gudisa Gebi
6 min readMar 27, 2024

--

Introduction

Bash, short for “Bourne Again Shell,” is a command-line shell and scripting language commonly used on Unix and Linux operating systems. It’s the default shell for most Linux distributions and macOS. Bash allows users to interact with the operating system through text-based commands, enabling them to execute programs, navigate the file system, manipulate files and directories, and automate tasks through scripting. It’s highly versatile and widely used by both casual users and system administrators due to its power and flexibility.

A Bash script is a plain text file which contains a series of commands. These commands are a mixture of commands we would normally type ouselves on the command line (such as ls or cp for example) and commands we could type on the command line but generally wouldn’t (you’ll discover these over the next few pages). An important point to remember though is:

Anything you can run normally on the command line can be put into a script and it will do exactly the same thing. Similarly, anything you can put into a script can also be run normally on the command line and it will do exactly the same thing.

It is convention to give files that are Bash scripts an extension of .sh (myscript.sh for example).

How do they work?

This is just a little bit of background knowledge. It’s not necessary to understand this in order to write scripts but it can be useful to know once you start getting into more complex scripts (and scripts that call and rely on other scripts once you start getting really fancy).

In the realm of Linux (and computers in general) we have the concept of programs and processes. A program is a blob of binary data consisting of a series of instructions for the CPU and possibly other resources (images, sound files and such) organised into a package and typically stored on your hard disk. When we say we are running a program we are not really running the program but a copy of it which is called a process. What we do is copy those instructions and resources from the hard disk into working memory (or RAM). We also allocate a bit of space in RAM for the process to store variables (to hold temporary working data) and a few flags to allow the operating system (OS) to manage and track the process during it’s execution.

Essentially a process is a running instance of a program.

There could be several processes representing the same program running in memory at the same time. For example I could have two terminals open and be running the command cp in both of them. In this case there would be two cp processes currently existing on the system. Once they are finished running the system then destroys them and there are no longer any processes representing the program cp.

When we are at the terminal we have a Bash process running in order to give us the Bash shell. If we start a script running it doesn’t actually run in that process but instead starts a new process to run inside. We’ll demonstrate this in the next section on variables and it’s implications should become clearer. For the most part you don’t need to worry too much about this phenomenon however.

How do we run them?

Running a Bash script is fairly easy. Another term you may come across is executing the script (which means the same thing). Before we can execute a script it must have the execute permission set (for safety reasons this permission is generally not set by default). If you forget to grant this permission before running the script you’ll just get an error message telling you as such and no harm will be done.

Here are the contents of myscript.sh

myscript.sh

#!/bin/bash

# A sample Bash script, by Gudisa

echo “Hello World!”

Let’s break it down:

  • Line 1 — Is what’s referred to as the shebang.
  • Line 2 — This is a comment. Anything after # is not executed. It is for our reference only.
  • Line 4 — Is the command echo which will print a message to the screen. You can type this command yourself on the command line and it will behave exactly the same.

The Shebang (#!)

The shebang, denoted by #!, is a special sequence of characters used at the beginning of a script file in Unix-like operating systems. It's followed by the path to the interpreter that should execute the script.

Importance of the Shebang:

  1. Interpreter Specification: The shebang informs the system about the interpreter to be used to execute the script. This is crucial because different scripting languages (e.g., Bash, Python, Perl) have different interpreters.

2. Cross-Platform Compatibility: By specifying the interpreter in the shebang, scripts can be written to work across different platforms without modification. For example, a script with #!/bin/bash at the top will run on any Unix-like system with Bash installed.

3.Executable Permission: When a script file has the shebang line, it allows users to execute the script directly without explicitly specifying the interpreter. However, the file must also have the executable permission set (chmod +x script_name.sh).

How the Shebang Works:

  1. Parsing: When you attempt to execute a script file, the system examines the first line to see if it begins with #!.

2.Interpreter Invocation: If the shebang is present, the system reads the interpreter path specified after #! and invokes that interpreter to execute the script.

3.Argument Passing: Any additional arguments after the interpreter path in the shebang line are passed to the interpreter when executing the script. For example, #!/bin/bash -x would enable debugging mode (-x) for the Bash script.

Example:

Consider a Bash script named hello.sh with the following shebang:

#!/bin/bash

When you execute ./hello.sh in the terminal, the system recognizes the shebang, uses /bin/bash as the interpreter, and executes the script accordingly.

Best Practices:

  1. Shebang Consistency: Always use the appropriate shebang for the scripting language used in the script. For Bash scripts, use #!/bin/bash, and for Python scripts, use #!/usr/bin/env python or #!/usr/bin/python3, depending on your needs.

2.Include Shebang in Every Script: Even if you’re writing a script for personal use or on a system where the shebang might seem unnecessary, including it ensures consistency and portability.

3.Choose the Correct Interpreter: Be mindful of the interpreter you specify in the shebang, as it determines how the script is executed.

It is possible to leave out the line with the shebang and still run the script but it is unwise. If you are at a terminal and running the Bash shell and you execute a script without a shebang then Bash will assume it is a Bash script. So this will only work assuming the user running the script is running it in a Bash shell and there are a variety of reasons why this may not be the case, which is dangerous.

Formatting

Formatting for the shebang was important (ie no spaces, must be on first line). There are many areas in Bash scripts where formatting is important. Typically it involves spaces and either the presence or absence of a space can be the difference between the command working or not. I’ll point these out as we encounter them. Also get in the habit of being mindful of the presence or absence of spaces when looking at code.

The main reason for this is that Bash was originally developed as an interface for Users to interact with the system and later extended to have more powerful scripting capabilities. Many decisions regarding it’s behaviour were made considering only the needs of the user and then scripting capabilities had to be worked in, later, around those decisions. People generally don’t mind this however as Bash scripts are still an awesome tool for quickly and easily joining existing programs into more powerful solutions.

Conclusion

In conclusion, Bash scripts offer a powerful means of interacting with Unix and Linux systems, enabling users to execute commands, manipulate files, automate tasks, and more. These scripts, written in the Bash scripting language, are simply plain text files containing a series of commands.

Understanding the fundamentals of Bash scripting, including the importance of the shebang (#!) and proper formatting, is essential for writing effective and reliable scripts.

--

--