Bash is a shell and command language that’s used in Linux. It’s the most popular shell, and it’s installed by default on most Linux systems. The Bash shell allows you to run programs, organize commands into scripts, and automate tasks through the use of programming constructs like conditionals, loops and functions. In this course we will be learning about basic bash scripting with examples such as:
What Is Bash in Linux?
Bash is a command interpreter, a shell and an interactive programming language. It can be used as an intermediate language to develop software.
Bash is the standard command line interface for GNU/Linux operating systems and it has been ported to Mac OS X, Windows 10, Android and other Unix-like systems.
How to Run a Bash Script
To run a Bash script, use the bash command. You can also use the source command to create and execute a new shell:
bash$ ././bin/bash
You can specify which file you want to execute using the -f parameter and then an absolute path or relative path. For example:
bash$ ./myScript.sh -f /home/user/Desktop#!/bin/bash
Exercise 1: Create Your First Bash Script
- Create a file called hello-world.sh and open it in your text editor of choice, such as nano or gedit .
- Write the following code:
#!/bin/bash
echo “Hello, World!”
Save the file and close your text editor when you’re done writing it!
Writing a Bash Script From Scratch
To write a Bash script from scratch, you’ll need to create a file with the .sh extension and then add the following:
- A shebang line that points to your bash interpreter
- A comment block that identifies what the script does
- A command (“function” in this case)
- A variable (here, $name)
Exercise 2: Add Comments
Commenting code is an important part of writing good code. It allows you to add notes that help explain what the code does, or why it was written in a certain way. This can be especially useful if someone else needs to know how your script works, or if you want to revisit the code later and figure out what was going on.
There are several different ways that comments can be added in Bash scripts:
- Commenting out a line – To comment out a single line in your script, enclose it with a pair of pound signs (
Exercise 3: Importing Libraries and Global Variables
Let’s start with a few simple examples of global variables. In this example, we’ve created three global variables and assigned them values:
- x = 10
- y = 20
- z = 30
Let’s look at how these are used in a script:
#!/bin/bash -e
x=$(($x+1)) y=$(($y+1)) z=$(($z+1))
echo $x,$y,$z
Exercise 4: Parsing Inputs to Your Scripts
A while loop is used to process the options.
This loop is controlled by getopts command.
Getopts reads a list of short option names and their arguments from the specified filename or from standard input, one option at a time, in turn. It exits with the exit status 0 if an error occurred before any input was processed; otherwise it exits with the exit status 1. If an error occurs after the first word has been read, getopts will indicate failure by generating a suitable diagnostic message on standard error and discarding remaining words from its input. In normal operation it will not generate diagnostic messages (and should be silent).
The while loop parses all options plus their corresponding arguments so you can use them later in your script.
Exercise 5: Working With Loops and Flow Control
Now that you know the basics of bash and how to work with loops, it’s time to learn more advanced bash programming techniques.
In this section, we’re going to first look at some common ways of iterating over arrays and repeating blocks of code. Then, we’ll cover how you can control the flow of your programs using break and continue , as well as case .
Exercise 6: Using Functions in Bash Scripts
- What is a function?
- How to define a function
- How to use a function
- How to call a function
- How to return values from a function
Conclusion
This exercise will show you how to work with variables in a bash script. The first step is creating a variable and giving it a value, which we do using the ‘set’ command. We then use this variable in our code by referencing its name prefaced with an exclamation point (‘!’) before performing operations on it.
This is demonstrated by executing commands like echo $! or substitution operators like ‘\$!’ instead of just plain old dollar signs. You should also know how to define other types of variables such as arrays, linked lists or dictionaries which store key value pairs within some form of data structure such as an array being sequential access while linked lists are non-sequential access structures but both allow us to put multiple values inside each element so long as they all share the same type (like numbers).