Basics of shell scripting

Samhith Vasikarla
4 min readAug 17, 2021

Shell Scripting

Definition;

Shell scripting is nothing but a sequence of shell commands which are present in a single file. When we execute a that shell script file we achieve some particular task

Some may get confused about the difference between programming and scripting.

Let’s see that difference now:

Programming needs compilation whereas scripting requires an interpreter. Mostly scripting is used to automate the tasks and programming is used to develop new features.

So now coming back to shell scripting, all shell scripting files end with ‘.sh’

Let’s consider our first basic shell program

The first statement #!/bin/bash is called a shebang statement. The importance of the shebang statement gives an absolute path to the bash interpreter. The bash interpreter will interpret the script even if we are using another shell.

If we don’t specify #!/bin/bash statement the script will default take as #!/bin/sh

Comments:

The shell script is commented out ‘prefixing # character’ for single-line comment.

Like the second line in the basic script in the above figure.

Multiline comments

We can comment on multiple lines using << and name of comment. We start a comment block with << and name anything to the block and wherever we want to stop the comment, we will simply type the name of the comment.

Printing characters on screen:

To print some text on the screen we use echo command

Statement : echo “ Hello”

Output: Hello

Types of shells:

First we discuss what a shell is.

Shell is a program which takes commands from the user, process the commands and executes the commands and returns the output to the user

They are different types of shells:

There are slightly different functions in each shell.

To go from one shell to another shell we type the name of shell and we get the environment of that particular shell

Just by giving command sh we go to the shell environment and by giving the name of the bash we again switched to bash terminal

Now we perform another task :

Task is to create a file, the file name is taken from the user and add a shebang statement at the start of the file.

So in this task we will learn how users can specify the arguments into the shell script.

The script code is shown below

read command is used to read the input given by the user. To access the user input in the shell script we use $ as prefix

Here file is a variable which takes the input from the user . So to access the input we use $file

After creating the file with touch command we are inserting shebang statement as first statement

Different types of quotations we use in the shell script and their purpose:

We use different quotations in the shell script, like double quotations,single quotations and reverse quotations

Reverse quotes is used to execute the command and the output is assigned to a variable or printed in the echo

Double quotes is used to print the values of variables and single quotes are used to print exact string means it does not evaluate the variable values

Output:

Variables in shell scripting:

Rules for variable names:

Variable names start with only alphabets, spaces are not allowed, special characters are not allowed except underscore(‘_’).

Variable names are case sensitive.

Special Variables in Shell Scripting:

$* — — : complete set of arguments as string

$# — — number of command line arguments

$1: — — first argument in the shell script

$2,…. $9 : second ,…. ninth command line argument

$0: file name

$$: process id of last background job

$? : exit status of last command

--

--