The Bash shell is one of the components of a Linux distro and a compelling reason to use Linux. Users can interact with Bash through the command line, and write scripts to automate tasks.
this may sound intimidating to the beginner, it is not difficult to get started with Bash scripting & can be very rewarding. In this short tutorial, we will go through various examples of Bash scripts to show you what it is capable of, and how to make use of different aspects of Bash.
The best way to learn about Bash scripting is by typing out some of the examples it will lead to faster learning. But by copying some of our examples onto your own system. You will also get a grasp of what is going on. you can Edit them, execute them, and learn as you go. Let’s get going
remember typing out each example will seem slower but you will learn faster than copy & paste
◦ How to write your script
◦ How to pass arguments to a Bash script
◦ How to use global and local variables in Bash
◦ How to read user input
◦ How to load and read arrays in Bash
◦ How to compare integers and strings
◦ How to detect file types in Bash
◦ How to use for, while, and until loops
◦ How to use functions in Bash
◦ How to use if statements
◦ How to use case statements
◦ How to use quotes and special characters in Bash
◦ How to perform arithmetic calculations with Bash
◦ How to use Bash redirection
“Bash is a command processor that typically runs in a text window where the user types command’s that cause actions. Bash can also read and execute commands from a file, called a shell script.”
simplified = press ctrl alt and the letter T will bring up a terminal where you can type your first few commands A Bash script is a set of commands that will be executed in a set order. The first shell script will use just print on the terminal, output:
Open your terminal with ctrl alt + T and type
echo Hello disregarding the
$ Sign at the beginning of the line, which indicates that you are supposed to type commands as a non-privileged user.
$ echo Hello Hello
What Is an Interpreter?
The next thing you know & keep in mind is that the Bash shell is an interpreter. What is an interpreter? In simple words, the interpreter is the process behind script execution capable of interpreting all commands within your shell.
Since we are writing a shell script, there are a number of shells you can choose from to interpret our commands on the Linux system, however in our case we choose the default shell, Bash ( Bourne-again shell ). First, let’s locate our Bash shell interpreter using the
which command to show the location of Bash interpreter on your system:
$ which bash /usr/bin/bash