The Bash Scripting Language
10 min readยทJan 1, 2025
In programming, a script is a text file that contains a series of instructions written in a scripting language that can be executed by a computer or shell interpreter to produce a desired result.
These scripts are primarily used to regroup multiple instructions together to automate repetitive or complex tasks that would be time-consuming or error-prone if done manually, one command at a time.
Common tasks often include custom CLI tools creation, data processing, file manipulation, software installation, automated testing, program compilation, and so on.
An overview of Bash scripts
In the context of Bash, a Bash script is a regular file that contains instructions written in the Bash scripting language and is executed by the Bash shell.
Bash scripts are easily distinguishable from other files and scripts, as they usually have a .sh file extension, for example script.sh, although it is not mandatory.
The shebang directive
Bash scripts always start with an interpreter directive called a shebang #! that indicates to the system that the script should be interpreted and executed using the Bash shell specified at the absolute /bin/bash path:
#!/bin/bash
This directive is essential as it ensures that the correct interpreter is used to execute the script, regardless of the shell it is executed from or the user's default shell settings.
Commands and execution order
A Bash script consists of a series of Bash commands that are read and executed by the Bash interpreter, from top to bottom.
More specifically, Bash executes the first command in the script after the shebang, then moves to the next line, and repeats the process until it reaches the end of the script or encounters an exit condition, such as an explicit exit command or an error.
#!/bin/bash
command_1
command_2
...
The list of commands includes all the available shell builtins, binaries, and operators, but also additional structures, such as variables, conditions, and loops, that allow developers to create more complex logic and behavior by altering the standard execution flow of the script.
This sequential execution makes Bash scripts easy to follow and debug, and allow developers to trace the script's behavior line by line.
Comments
In Bash scripts, comments are lines that are ignored by the interpreter, and therefore not executed, but are used to provide explanations, documentation, or notes that improve the script readability and make it easier for other developers (or your future self) to understand what it does.
They start with the # character and can be placed anywhere in the script:
# This is a comment
#!/bin/bash
# This is a comment
echo "Hello World"
# This is a comment
Whitespaces and indentation
Similar to when running commands in the command-line interface, whitespaces, including spaces and tabs, can be used to separate and format elements in the script.
#!/bin/bash
echo "Hello World"
echo "Hello World"
echo Hello World
Note: While proper indentation and spacing improve code readability โ especially within control structures and functions โ they do not influence the way the script is executed.
Script execution
A Bash script can essentially be executed in two ways.
It can either be executed by passing its relative or absolute path as an argument of the bash binary:
$ /bin/bash script.sh
Or by giving it execution permissions using the chmod command and running it as an executable from its working directory:
$ chmod +x script.sh
$ ./script.sh
Note: The second method is often preferred as it allows to make the script more user-friendly, consistent, and secure.
Making Bash scripts available system-wide
To make a Bash script executable from anywhere within the system, you can move or copy it in one of the directories listed in your PATH environment variable, such as /usr/local/bin or /usr/bin, which usually contains system-wide executable scripts and commands:
$ cp my_script.sh /usr/local/bin
And then execute it as a regular shell command using its name:
$ my_script.sh
Creating a scripts directory
As an alternative, if you already have a directory containing all your scripts, you can add the path of that directory to the PATH environment variable by opening your ~/.bashrc or ~/.bash_profile configuration:
$ vim ~/.bashrc
Copy-pasting the following directive into it:
export PATH=$PATH:/path/to/scripts
Saving the file and reloading your shell's configuration using the source command:
$ source ~/.bashrc
Defining a command alias
Finally, you can define an alias with a custom name that when executed will invoke you script:
alias my_script="/home/username/scripts/my_script.sh"
And execute it using its alias:
$ my_script
Note: When using this method, if you decide to change the name of the script, you will also have to update its alias as it will otherwise point to an invalid filepath.
Writing your first Bash script
In this final section, you'll write and execute your first Bash script that will automatically create a new directory named tmp, then write the string "Hello World" into a file named hello.txt within it.
Step 1: Creating a scripts directory
First, let's create a new directory named scripts within your home directory using the mkdir command:
$ mkdir ~/scripts
And enter it using the cd command:
$ cd ~/scripts
Step 2: Creating a script file
Within that directory, let's create a new file named hello.sh using the Vim text editor:
$ vim hello.sh
And write the following instructions into it:
#!/bin/bash
# Create a new directory named "tmp"
mkdir tmp
# Write "Hello World" into a file named "hello.txt" in the "tmp" directory
echo "Hello World" > tmp/hello.txt
# Write the content of the "hello.txt" file to the standard output
cat tmp/hello.txt
Finally, let's save and quit this file using the :wq command:
:wq
Step 3: Changing the script's permissions
Let's give the script execution permissions using the chmod command:
$ chmod +x hello.sh
Step 4: Executing the script
Let's execute the script in the local directory:
$ ./hello.sh
Hello World
And verify the existence of the files created by the script using the ls command:
$ ls -R
hello.sh tmp
./tmp:
hello.txt
Step 5: Cleaning up the script
Finally, let's clean up the temporary tmp directory using the rm command:
$ rm -r tmp
๐ Congratulations! You've officially written and executed your first Bash script.
Summary
Here's a summary of what you've learned in this lesson:
- A Bash script is a file that contains instructions written in the Bash scripting language and is executed by the Bash shell.
- The
#!/bin/bashshebang directive is used to indicate that the script should be interpreted and executed using the Bash shell. - Bash executed the commands sequentially, line by line, from top to bottom.
- Comments are ignored lines that provide additional information about the script and start with a
#character. - The
chmod +xcommand is used to give a script execution permissions. - A script can be run from its local directory using the
./script.shcommand.
Enjoying the courses?
I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:
Support BackendBrewery