Bash Script -Tutorial Part_1

Gokul
2 min readDec 30, 2022

--

Bash Script

BASH is an acronym for Bourne Again Shell.

Bash is a shell program written by Brian Fox as an upgraded version of Bourne Shell program ‘sh’.

Bash is the default login shell for most Linux distributions and Apple’s mac OS.

There are different types of shells available in Linux Operating Systems.Some are,

  1. Bourne Shell
  2. C shell
  3. Korn Shell
  4. GNU Bourne Shell

To know where bash is located in your OS, type the below command.

    which bash   
cat /etc/shells

Hello World! Bash Script

  1. Open any editor in you Linux machine
  2. create file with .sh extension
  3. Then Code Your Script

echo “ Hello World! “

4.change the file into executable mode

chmod +x path/filename.sh

5.Compile and Execute the file using the below command

./filename.sh

Bash Comments

Bash Single Line Command

#single line command.it doesn’t execute

<<COMMENT Multiple Line Command COMMENT

Bash Variables

Variables are the containers which store data as the value inside them.

The variables are accessed using Dollar Symbol ( $ )

Syntax

Variable_name =value

Example

  1. year=2012
  2. comp_name=Kongunadu

Types of Bash Variables

1.System-Defined Variables

These are the pre-defined variables as they are created and maintained by the LINUX operating system itself.

example :#! /bin/bash

# Bash System-defined Variables

echo $HOME # Home Directory

echo $PWD # current working directory

echo $BASH # Bash shell name

echo $BASH_VERSION # Bash shell Version

echo $LOGNAME # Name of the Login Use

echo $OSTYPE # Type of OS

2.User-Defined Variables

These variables are created and maintained by the user.

Read User Input

To read the Bash user input, we use the built-in Bash command called read.

Syntax

read <variable_name>

read -p “username:” user_var

echo “The username is: “ $user_var

Bash Date Formatting

Bash shell provides different date commands along with different formatting options. We can use those commands to format Bash date to a required one.

date #This Command display the current date in the System

For More Commands refer this tutorials

https://www.javatpoint.com/bash-date-format

Bash Sleep

Syntax

sleep number[suffix]

Examples

  1. sleep 9 or sleep 9s
  2. sleep 0.5 or sleep 0.5s
  3. sleep 2m 30s
  4. sleep 8h
  5. sleep 2d 9h 5m 55s

Thank You !!!

Charge and Update the Brain and Knowledge in Daily Basis

--

--

Gokul
Gokul

Written by Gokul

Cybersecurity Enthusiast | Smart India Hackathon |TN Police Hackathon Finalist | Linux | WebApp Penetration Tester | CCNA |Intern At Coimbatore CyberCrime Dept

No responses yet