Bash Scripting

Isha JainIsha Jain
3 min read

Introduction

Bash means “Bourn Again Shell” . A Bash script is a file with steps that are executed on the Bash shell, one line at a time. These scripts are easy and effective to write. They help automate tasks and make your work more efficient. Bash scripting was originally designed for Linux systems, but over time, its use with Windows has also increased. These scripts contains a series of commands and interpreter executes these commands.

Bash scripts starts with #!/bin/bash - this line is called shebang . This line is called shebang because “#” sharp and “!” is referred as bang. The purpose of this line is to tell operating system which interpreter to use.
Other types of interpreters that can be used -

  • #!/bin/csh

  • #!/bin/ksh

  • #!/bin/sh

  • #!/user/bin/pwsh

Initial Steps to create and run file :

  • To create a file - touch <file-name>
    If the file is created with .txt extension then this will not be executable file. The file extension that work well with bash scripts are .sh , .py , .bash , .pl and others. These extension allow interpreter to identify that these files are executable.

  • To make the file editable there are multiple ways -

    chmod 755 <filename>

    chmod +x <filename>

    chmod +w <filename> → +w to add write permission , -w to remove write permission , u+w →give user the write permission

  • To open the file to add scripts → nano <filename>

  • To save file - ctrl x → y → press Enter

To run file - ./<filename>

Your First Bash Script

Writing “scripting is fun“ as your first script :

Variables

Variable are storing location that have a name. These are similar to key value pair. Variables are case sensitive and by nomenclature variable are always upper case.

Nomenclature for variable name:

Valid names are as below -

name="John"

_name="Jane"

Name1="Doe"

USER_AGE=25

Invalid variable names are as below -

1name ( starts with a number)

first-name (contains hyphen)

user name (contains spaces)

my$name( contains special character $)

To use the variables we use “$” followed by variable name.

If we use ${variablename} then the variable can be used as string

Example - MY_SHELL = “Shell” (this is wrong since it includes spaces)

MY_SHELL=”Shell” (this is the correct way)

echo”${MY_SHELL}ing” - this will give output as - Shelling

If we put space then MY_SHELL will not get printed.

After removing extra spaces whole line will get printed.

Remove Variable

To remove variable we use : unset <variable name>

Other Ways to Use Variables

There are other different ways that variables can be used which can be seen in below repository .

https://github.com/ishaj72/Bash-Scripting

(Other connected topic like functions , if else conditions etc will be published in different blog and link will be added later here)

Happy Read!!

0
Subscribe to my newsletter

Read articles from Isha Jain directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Isha Jain
Isha Jain

An enthusiast about continuously broadening my horizons in the dynamic world of technology.