what is shell scripting used for

what is shell scripting used for

In case you are conversant in a programming language like python or C, then it must be fairly straightforward to get began with shell scripting. This text can be utilized as a “cheat sheet” as nicely.

Shell Scripting Picture Supply:etlhive

You're reading: what is shell scripting used for

Shell Scripting

The shell is a program that gives the person with an interface to make use of the working system’s features by means of some instructions. A shell script is a program that’s used to carry out particular duties.

Shell scripts are principally used to keep away from repetitive work. You possibly can write a script to automate a set of directions to be executed one after the opposite, as an alternative of typing within the instructions one after the opposite n variety of instances.

On this article, we’ll undergo some instructions for BASH (Bourne Once more SHell), which is probably the most broadly used shell and likewise the default in most Linux methods, which most servers use as their working system. Shell scripting is especially helpful for DevOps and backend growth.

Fundamental instance

A typical script would possibly appear to be this:

#!/bin/bash echo “Welcome!! Please Enter Your Identify” learn title echo “Hi there $title”

Let’s break down the above code:


A line beginning with #! is used to inform the os which interpreter to make use of to execute the file. There are numerous shell interpreters like ksh, zsh(macOS), csh(C shell), ssh(safe shell), and many others. The above line asks the system to make use of the bash interpreter.

echo “Welcome!! Please Enter your Identify”

The echo key phrase is used to output the strings that it’s being handed as arguments. It outputs onto the stdout.

learn title

The learn command is used to obtain enter whereas working the script. After we wrote learn title it initialized a variable named title and saved the enter in it.

echo “Hi there $title”

The $ image is used to print the worth of a variable. Within the above instance, it prints no matter enter was given within the earlier step.

Executing the file

Now that we have now seen what a typical shell script appears to be like like, let’s take a look at how you can execute the file.

  1. Save the file with a .sh extension
  2. To execute the file, first we have to give it execute permissions. chmod +x filepath/newurbanhabitat.com
  3. To execute the file, we are able to do it within the following methods
    • In case you are utilizing a GUI file navigation system, right-click on the file and click on on run or execute.
    • In case you are utilizing the terminal, ./newurbanhabitat.com will execute the script. (Be sure to are within the appropriate listing!) Screenshot

Now, let’s talk about shell scripting in slightly extra element.


Variables in shell scripting are just like variables normally programming languages; they’re used as a pointer to the precise information. Variables should not have to be declared, as in comparison with programming languages like C, however when you attempt to learn from an undeclared variable, then you’ll not get meant outcomes.

my_name=”Rohan Reddy” echo $my_name

Naming conventions of variables

  1. All caps and underscores for exported variables and constants, when they’re shared throughout a number of scripts. Ex: JOB_ID, PROCESS_NAME
  2. All lowercase and underscores for all variables which are scoped to a single script. Ex: max_amount , i.
  3. Use main underscore for personal variables and features; can be utilized the place features share the identical variables with out clashing with equally named variables within the code. Ex: _debug

Read: what is the difference between adjective and adverb

If you end up declaring variables, ensure that there are not any areas earlier than and after =. (a=12 is appropriate; a = 12 throws an error.)

Particular Key phrases and Variables

$PWD, this variable accommodates the worth of the Current Working Listing

Arguments handed from the command line may be accessed through the use of $0, $1, $2, … notation. $0 is the title of the script itself, $1 is the primary argument, $2 is the second argument and so forth.

./script 1 2 3

Be aware: These variables not solely work from the command line, but additionally with features. It is best to use features to arrange your bash code identical to your would in one other language. Variables may be handed to features identical to you do with the command line, utilizing $1, $2, and many others.

On the finish of your script, strive one thing like:

if [ -z “$@” ]; then major else “$@” fi

This may will let you take a look at particular person features like ./newurbanhabitat.com function1 or ./newurbanhabitat.com function2 arg1, and have your “major” entry operate run by default.

  • $0 = scriptname
  • first_argument = $1: We will assign it to variables
  • $#: This variable accommodates the variety of arguments provided to the script.
  • $?: The exit standing of the final command executed. Most instructions return 0 in the event that they have been profitable and 1 in the event that they have been unsuccessful.
  • Feedback in shell scripting begin with # image.

Management-flow Constructs

If-else statements

Easy if assertion

if [expression] assertion if

Easy if-else assertion

if [expression] then command1 else command2 if

if-else_if-else assertion

if [expression] then statement1 else [expression2] then statement2 else statement3 if

Nested if statements

if[expression1] then if [expression2] then statement1 else statement2 if else statement3 if


The change assertion is a multiway department assertion. It supplies a straightforward technique to dispatch execution to completely different elements of code based mostly on the worth of the expression. In shell scripting, we have now the case command for this.

case “$var” in #case 1 pattern_1) statement_1;; #case 2 patter_2) statement_2;; case


id=1 case $id in #case 1 1) echo “ID is 1”;; #case 2 2) echo “ID is 2”;; case


Loops are highly effective instruments that assist us execute a set of instructions repeatedly.

You might want to know: what is the difference between prayer and meditation

Whereas loop executes the given instructions till the given situation turns into false.

whereas situation do assertion completed


#!/bin/sh vehicles=10 whereas [ “$cars” -gt 0 ] do vehicles=`expr $vehicles – 1` echo $vehicles completed

When writing shell scripts, be conscious of the place areas ought to exist and the place there must be no areas.

For loop is an environment friendly approach of writing loops. Syntax:

for i in vary do command completed


We will loop over a spread of numbers, loop over an array of numbers, or loop over the output from a command.

Instance 1:

for i in 1 2 3 4 5 do echo $i #prints 1 to five completed

Instance 2:

for i in {1..5} do echo $i #can be utilized to specify vary completed

Instance 3:

#!/bin/bash for (( c=1; c<=5; c++ )) #this is among the methods we are able to write loops which is analogous to C-style loops do echo “Welcome $c instances” completed

Break & Proceed

break and proceed key phrases can be utilized with the identical which means as any programming languages. break is used to cease the execution of a loop, and proceed can be utilized to trigger the loop to execute the following iteration.

We’ll discuss extra superior matters like sed, awk and common expressions within the subsequent publish.

Extra Useful Ideas


Placing set -o xtrace and set -o errexit can typically assist debug bash scripts. xtrace will print every bash line as they run, and errexit will exit the script if a command returns a standing of one thing aside from 0. ‘$?’ will present the final standing code.

For instance, embody this on the high of your scripts:

take a look at -z “${DEBUG}” || set -o xtrace set -o errexit

The primary line checks if a DEBUG surroundings variable is about, and whether it is, it’ll print every bash line. The second makes the script exit when there was an error on a command.

Vital Be aware

There are numerous scripting languages, and practically any programming language can be utilized to write down scripts (python, ruby, go, javascript, and many others.). These have many benefits over bash with regard to information manipulation and ease. Bash has the benefit of working on practically any Linux machine with out prior set up effort. At all times contemplate the complexity of your scripting activity and ask if it’s actually mandatory to make use of bash. Bash ought to largely be reserved for duties the place one other language could be cumbersome.

Read more: what is 20 dollars worth of weed called

Extra Assets and References

  • The Linux Documentation Mission
  • https://www.tutorialspoint.com/unix
  • ShellCheck – may be put in as a VS Code extension to catch many bash errors which are straightforward to make.