Bash Scripting for Beginners Part-1
By Prgmaz 23 May 2021

THUMBNAIL HERE

What we need for this tutorial

  • A Latest Windows (if you have ubuntu installed skip to the commands part)
  • WSL or WSL2
  • Ubuntu Subsystem installed from the Windows store.
  • You need to know basic of linux commands.

Installing WSL on Windows

To install the WSL(Windows Subsystem for Linux) on Windows, you need to make sure that you’re running latest version of Windows.

Install WSL by opening powershell with admin priviledges and typing this command.

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

This command will enable WSL Feature on Windows. You can restart your system now and skip next step.

Upgrading to WSL2

In this tutorial, We are gonna install WSL2, so Extra steps will be needed.

To enable WSL2, You need to enable another feature by typing this command in powershell window with admin priviledges.

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

After enabling this feature, RESTART your System.

POWERSHELL Image HERE

Now download wsl2 package from this link, Download From Here and Install it.

Now, Let’s set wsl default version to 2 by typing this command in powershell with admin priviledges.

wsl --set-default-version 2

Now WSL2 is now installed on our sytem.

Installing Linux Distribution

In this tutorial, I’m gonna install Ubuntu (You can install other linux as well). Go to Microsoft Store and download Ubuntu.

I’m also going to install Windows Termnial. You can download this from Windows store too.

Ubuntu Image HERE After Downloading, Launch it. It will ask for creating a new user.

Ubuntu Password IMage here

Now you have successfully installed the Ubuntu on Windows.

What is Bash?

Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. First released in 1989, it has been used as the default login shell for most Linux distributions

How to create and start writing scripts in Bash?

First You need to create a file for which extension would be .sh, You can create it by typing sudo nano <filename.sh>.

Now you have created your first bash file. Now Let’s talk about SheBang line.

What is Shebang line?

A shebang is the character sequence consisting of the characters number sign and exclamation mark at the beginning of a script. It is also called sha-bang, hashbang, pound-bang, or hash-pling.

This tells the system, which interpreter to use to run this file. Default is /bin/sh, But It is good to write this.

Shebang Line

Script to print “Hello World!”

I’m going to create program1.sh file by typing sudo nano program1.sh and start writing this code.

#!/bin/bash

echo "Hello World!";

Save it by pressing CTRL+X and pressing ENTER.

To Run it, First You need to give it execution permission first, type sudo chmod +x program1.sh. Now if you type ./program1.sh in your terminal, Your file will run.

Script to Enter name from terminal and Printing “Hello, Name”

Create a file program2.sh and start writing this code.

To Read from the Terminal, we use read variable_name statment, here variable_name can be anything.

#!/bin/bash

echo "Enter Name: ";
read name; # This is command to read value from terminal and store it in `name` variable.
echo "Hello, $name"; # $name is used to get the data from the variable.

Shebang Line

Here line after # are comments except the first line.

You can also create variables by this Syntax. <variable_name>=<value>;

Conditional Statements

A basic if statment is a statement which executes if and only if this given expression is true.

Syntax

: #You can use multiple expression by combining them [expression1] && [expression2] or [expression1] || [expression2] if [expression1] then statements elif [expression2] then statements else statements fi

For Example: Let’s create a program which takes input from user a number and if that number is greater than 50, then print “Hello” else print “Good bye”

#!/bin/bash

read number;

if [ $number -gt 50 ]
then
    echo "Hello"
else
    echo "Good bye"
fi

Shebang Line

Here -gt checks whether the left side value is greater than right side value. There are more below.

Operators Description
-eq Checks if the value of two operands are equal or not; if yes, then the condition becomes true.
-ne Checks if the value of two operands are equal or not; if values are not equal, then the condition becomes true.
-gt Checks if the value of left operand is greater than the value of right operand; if yes, then the condition becomes true.
-lt Checks if the value of left operand is less than the value of right operand; if yes, then the condition becomes true.
-ge Checks if the value of left operand is greater than or equal to the value of right operand; if yes, then the condition becomes true.
-le Checks if the value of left operand is less than or equal to the value of right operand; if yes, then the condition becomes true.
Let’s create a program that gets the 3 subject marks and calculate Average and Total marks.

Create a file named program4.sh and write this code.

#!/bin/bash

echo "Enter Marks-";
read marks1;
read marks2;
read marks3;

((total = $marks1 + $marks2 + $marks3)); # To use math expression, we enclose them with parenthesis.
((avg = $total / 3));
echo "Total: $total";
echo "Average: $avg";

Shebang Line

That is all for today, See you in the next part.

Follow me on my socials.

Naman Baranwal
Hi, I’m Prgmaz. I’m a Web, Game and App Developer.

Tags

  • bash
  • linux
  • commands
  • scripting
  • programming