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:
$ ps -p $$
The output is as follows:
PID TTY TIME CMD 12578 pts/4 00:00:00 bash
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:
$ bash
Also, your shell will be bash
now. To make bash
as a default login shell, run the following command:
$ chsh -s /bin/bash
The output obtained is as follows:
Changing shell for user. Password:****** Shell changed.
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:
#! /bin/bash
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:
$ chmod u+x foo
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:
Whenever we learn a new programming language, we first learn how to write the Hello World program in it. It is the best way to know and interact with a new language. This also helps in confirming that the basic environment for a program in a given language has been set up and you are good to dive deep inside this language.
We can print the output of commands in console in an interactive way. Console is also known as a standard input and output stream. To print anything in a bash
console, use the echo
command followed by what is to be printed:
$ echo Hello World Hello World
Alternatively, put the text to be printed in double quotes:
$ echo "Hello World" Hello World
You can also put the text to be printed in single quotes:
$ echo 'Hello World' Hello World
We can also use the printf
command in shell programming for printing. The printf
command also supports formatted printing, similar to what we have in C programming language— the printf( )
function:
$ printf "Hello World" Hello World$
Here, after the output, we see the command prompt ($
) because printf
doesn't add a default newline after execution while echo does. So, we have to explicitly add the newline (
) in the printf
statement to add a newline:
$ printf "Hello World " Hello World
Similar to the C printf( )
, we can specify formatted printing in bash
. The syntax of bash
printf
is as follows:
printf FORMAT [ARGUMENTS]
FORMAT
is a string that describes the format specifications and is specified within double quotes. ARGUMENTS
can be the value or a variable corresponding to format specification. Format specification consists of the percentage (%
) sign followed by format specifier. Format specifiers are explained in the following table:
Format specification |
Description |
---|---|
|
This prints an unsigned integer value |
|
This prints an associated argument as a signed number |
|
This prints an associated argument as a floating point number |
|
This prints an unsigned octal value |
|
This prints a string value |
|
This prints an unsigned hexadecimal value (0 to 9 and A to F) |
|
This prints an unsigned hexadecimal value (0 to 9 and a to f) |
The following examples demonstrate how to use format specification for printing different data type format in shell:
$ printf "%d mul %f = %f " 6 6.0 36.0 6 mul 6.000000 = 36.000000 $ printf "%s Scripting " Shell Shell Scripting
We can also optionally specify a modifier in format specification to align an output to provide better formatting to the output. Format modifiers are placed between %
and the format specifier character. The following table explains format modifiers:
Format Modifiers |
Description |
---|---|
N |
This is any number that specifies a minimum field width. |
. |
This is used together with field width. The field doesn't expand when the text is longer. |
- |
This is the left-bound text printing in the field. |
0 |
This is used to fill padding with zeros (0) instead of whitespaces. By default, padding is done with whitespaces. |
The following example demonstrates how to use format modifiers to improve printing formatting:
$ printf "%d mul %.2f = %.2f " 6 6.0 36.0 6 mul 6.00 = 36.00
Interactive printing is good if we have to print one or two lines, but for a lot of printing, it's good and preferred to write a script file. A script file will contain all the instructions and we can run a script file to perform the needed task.
Now, we are going to create a bash
script file that makes use of the echo
and printf
commands and print messages:
#!/bin/bash #Filename: print.sh #Description: print and echo echo "Basic mathematics" printf "%-7d %-7s %-7.2f = %-7.2f " 23 plus 5.5 28.5 printf "%-7.2f %-7s %-7d = %-7.2f " 50.50 minus 20 30.50 printf "%-7d %-7s %-7d = %-7d " 10 mul 5 50 printf "%-7d %-7s %-7d = %-7.2f " 27 div 4 6.75
The first line in bash
script represents the path of the interpreter used. The second line is a comment line telling the filename of a script file. In shell script, we use #
to add a comment. Furthermore, the echo
command will print strings written within double quotes. For the rest, we have used printf
to print formatted output.
To run this script, we will first provide execute permission to a user/owner of this script:
$ chmod u+x print.sh
Then, run the script file in console as follows:
$ ./print.sh
The result after running this script will look as follows:
18.224.73.77