Search

Getting started with Shell scripting 

A shell script is a computer program that can be executed in a Unix/Linux shell. It allows you to execute commands from a file that can be used for file management. A shell in a Linux OS may take the input in the form of commands and produce the output in the terminal itself. Furthermore, shell scripts can help to automate tasks that creates an unnecessary workload.

Writing a Shell script can be a piece of cake for programmers but people who are not familiar with programming may initially face some difficulties. So let’s begin with writing a shell script!

Writing a shell script

To write a shell script, you can use any text editor. We prefer to use “sublime text” but it’s really up to your preferences and convenience.

But before beginning, we have provided the commands with their meanings that we will be using in our scripts which can be useful for absolute beginners.

echo – used for printing a text

read – used for taking inputs

For writing a shell script, we have provided a few steps that would help you to ease your way out to directly start focusing on writing the code:

1) Create a file using a text editor with the extension “.sh”

2) Start the script with #! /bin/sh

3) Start writing your code

“#!” is an operator called shebang which directs the script to the interpreter location. So, if we use”#! /bin/sh” the script gets directed to the bourne-shell. 

Let’s begin with our first script:

#! /bin/bash

echo “Hello world”

To run the script, you need to go to the file location in the terminal and type bash [filename]

Further, you can use variables to take inputs but it requires certain rules to be followed.

  1. The variables should always be in uppercase
  2. There should be no space after a “=” sign
  3. Variables cannot includes numbers and underscore in the beginning. However, underscores can be added in between or at the end.

Below we have provided a script to take user input asking the name of a person and returning with an output.

echo ”Please enter your full name”

read FNAME LNAME

echo “Your name is $FNAME $LNAME”

That’s it for this blog; However, in the upcoming blogs we will be showing you how you can create your custom scripts that may ease your work in Linux.

Author: Mohammad Usman Rais (IT Security Engineer)

Table of Contents

Social Media
Facebook
Twitter
WhatsApp
LinkedIn