Chapter 1. The Beginning of the Scripting Journey
Unix, Unix-like, or Linux-based OS provide a lot of powerful features to work upon. Among them, the most powerful and important feature is executing a wide range of commands to perform a task quickly and easily; for example, ls
, cat
, sort
, grep
, and so on. We will come to know about a subset of commands and usages throughout this book. In order to run a command, we need an interface that is widely known as shell.
Shell is a program that acts as an interface between the users (we) and the OS kernel (Linux, Unix, and so on). Understanding in terms of Windows OS, shell serves a similar purpose DOS does. Different shells are available for Unix, Unix-like, or Linux OS. Some of the popular shells are Bourne shell (sh), C shell (csh), Korn shell (ksh), Bourne Again shell (bash), and Z shell (zsh).
In this book, we will be using Linux OS and Bourne Again shell, popularly known by its acronym bash
. Linux-based systems generally have bash
already installed. In case bash
is not installed, try installing the bash package from your distribution's package manager. In order to know which shell currently your Linux console is using, run the following command in terminal:
The output is as follows:
In the preceding ouput, we see that the CMD
column has value bash
. This means, we are currently using bash
shell in our current console.
If your console is not using the bash
shell, then you can run the following command:
Also, your shell will be bash
now. To make bash
as a default login shell, run the following command:
The output obtained is as follows:
We are now set with bash
shell and ready to learn shell scripting in detail. Shell scripts are nothing but plain text files with a series of commands that are run by bash
in a specified order. Writing shell scripts is very useful when you have to perform a series of tasks by running various commands, as bash
will read each line from a script file and run it without any need of user intervention. The general file extension used for shell scripts are .sh
, .bash
, .zsh
, .ksh
, and so on. Rather than using a file extension for shell scripts, it's preferred to keep a filename without extension and let an interpreter identify the type by looking into shebang (#!
). Shebang is used in scripts to indicate an interpreter for execution. It is written in the first line of a script file, for example:
It means use the bash
shell to execute a given script. To run a shell script, make sure it has execute permission. To provide execute permission to an owner of a file, run the following command:
Here, foo
is the shell script file. After running this command, foo
will have execute permission for the owner of the file.
Now, we are ready to proceed further on learning shell scripting concepts in detail. Each topic and subtopic covered in the chapters with examples will lead us progressively towards a good shell script programmer.
In this chapter, we will talk broadly about the following topics:
- Hello World in shell
- Define variables of choice
- Builtin shell variables
- Operators
- Shell expansions
- Construct commands using eval
- Make bash behave using set