Introduction Bash Shell Scripting
Introduction Bash Shell Scripting
Abstract
This guide develops an introduction to the console programming lan-
guage, in this case we will use Bash shell script, we explain the structure
of these programs, how to execute them and some of the applications of
these.
1 Introduction
What is Bash?
Bash is a ”Unix shell”: a command line interface for interacting with the oper-
ating system. It is widely available, being the default shell on many GNU/Linux
distributions and on Mac OSX, with ports existing for many other systems. It
was created in the late 1980s by a programmer named Brian Fox, working for
the Free Software Foundation. It was intended as a free software alternative to
the Bourne shell (in fact, its name is an acronym for Bourne Again SHell), and
it incorporates all features of that shell, as well as new features such as integer
arithmetic and job control.[1]
In addition to the interactive mode, where the user types one command at
a time, with immediate execution and feedback, Bash (like many other shells)
also has the ability to run an entire script of commands, known as a ”Bash
shell script” (or ”Bash script” or ”shell script” or just ”script”). A script might
contain just a very simple list of commands - or even just a single command - or
it might contain functions, loops, conditional constructs, and all the other hall-
marks of imperative programming. In effect, a Bash shell script is a computer
program written in the Bash programming language.
Shell scripting is the art of creating and maintaining such scripts.
Shell scripts can be called from the interactive command-line described above;
or, they can be called from other parts of the system. One script might be set
to run when the system boots up; another might be set to run every weekday
at 2:30 AM; another might run whenever a user logs into the system.
Shell scripts are commonly used for many system administration tasks, such as
performing disk backups, evaluating system logs, and so on. They are also com-
monly used as installation scripts for complex programs. They are particularly
suited to all of these because they allow complexity without requiring it: if a
1
script just needs to run two external programs, then it can be a two-line script,
and if it needs all the power and decision-making ability of a Turing-complete
imperative programming language, then it can have that as well.[1]
The first row of the file indicates the interpreter that is used to execute the
file. then the program is written using the language of the console. In this case,
the program prints a ”hello world”. To write a comment, write before the sign
as shown in the second line.
2
3.1 Redirect
The input can be changed (stdin) /(stdout) standard output or the error output
(stderr)of any order.
3.2 Variables
In all scripts you will have to work with variables, sooner or later. Let’s see how
they are defined and used. A good habit when we define variables in Bash is to
use uppercase letters, this is not necessary, but it will help us to have a script
easier to understand.
The variables in Bash are defined as NAME=valor (no spaces before or after
the symbol ’=’) and its value is used, setting the symbol ’$’ in front of the name
of the variable, $NAME.
3
If when using the value of a variable, the variable name is followed by a
character that is another letter, number or symbol’ ’, we will have to use the
symbols ’’ around the name of the variable.
You can also use in our scripts, variables that we have not defined ourselves.
These variables are the so-called ’environment variables’ of the system.
There are a series of characters that have a special meaning in Bash, for
example $ y ”. If we want to literally use these characters in the value of a
variable we will have to use the symbol ’ı́n front of them.
If we want to define numeric variables for their use in Bash scripts we can
use the command let. Nothing better than an example to see how to work with
numerical variables.
4
3.3 Input and output data
To show data we already know the order echo, there is also another way to show
the output with the printf command. to read data there is a command called
read, let’s illustrate the input and output of data with the following example:
3.4 Conditional
Conditional structures allow you to decide if an action is taken or not; This
decision is made by evaluating an expression. To understand it better, let’s
observe the following examples:
3.5 Loops
In this section you will find the for, while and until loops.
The for loop is different from those of other programming languages. Basically,
5
it allows you to iterate over a series of ‘words’ contained within a string.
The while loop executes a piece of code if the control expression is true, and
only stops when it is false (or there is an explicit interruption within the running
code).
The until loop is almost identical to the loop loop, except that the code is
executed while the control expression is evaluated as false.
• -ne: Different
6
3.6 Functions
In Bash you can define functions. A function in Bash (called subroutine or
procedure in other programming languages) could be defined as a script within
a script. It serves to organize a script in logical units so that it is easier to
maintain and program it. In Bash the functions can be defined as follows:
4 Exercise
-Make a script called ”new” that requests a number and, if it is greater than
200, show the message: greater than 200 .
-Make a script called a ”question” that shows on the screen the question: Who
discovered America? and, according to the answer, show the message is correct
or is not correct.
-Create a script called ”pairs” that asks for a number and say if it’s even or
odd.
References
[1] Bash Reference Manual gnu.org 2019
[2] GNU Bash Reference Manual for Bash 3.2 network-theory.co.uk 2019