Bash for Everyone — Part 1

Sahil Ahamad
Jan 3, 2019 · 7 min read

Hi Guys,

This bash cookbook I created while learning the bash recently. I’ve put a lot of efforts in assembling the useful information and resources. I’ve divided this cookbook into two parts, first, one will be focused on bash and the second one will be on Unix commands and useful one-liners.

Table of Contents

Part-1 — Shell Scripting

3. Bash scripts.

4. Part-2

Shell scripting.

Shell scripting is a computer programming which contains the series of Unix commands.

A shell is a command-line interpreter and typical operations performed by shell scripts include file manipulation, program execution, and printing text.
Types of shells.

Shell Startup Process — BASH/SH/ZSH Startup Process.

Shell Startup — Sh/Bash/zsh | Source —

Valid Login shell for my machine —

cat /etc/shells

Type of shell.

  • /bin/bash — Bourne Again shell
  • /bin/csh — C shell
  • /bin/ksh — Korn Shell
  • /bin/sh — Bourne Shell, The original shell still used in UNIX.
  • /bin/tcsh — TENEX C Shell, a superset of the common C shell.

When the shell is started, it read the configuration files, The most commons are.

  • /etc/profile
  • ~/.bash_profile
  • ~/.bashrc
  • ~/.bash_logout — Contains the instructions for logout procedure. (don’t f*ck with it)

Usefulness of shell scripting

  • They remember commands.
  • They allow you to reuse the commands.
  • they let you edit the command.
  • Shells are highly programmable.


Pipelines are one or more simple commands (separated by the | symbol connects their input and output),

ls -l /usr/bin | less | sort | uniq | wc -l

Bash — Bourne Again Shell

Bash is a standard shell, which is intuitive and flexible. In Linux and MacOS, Bash is a standard shell for common users. Bash is also a superset of the Bourne shell.

The primary purpose of bash is to allow you to interact with the computers’ OS so that you can accomplish whatever you need to do. Bash is just a language to execute the command.

Bash Built-in commands

alias, bind, builtin, command, declare, echo, enable, help, let, local, logout, printf, read, shopt, type, typeset, ulimit and unalias.

The main use of the bash is to allow you to interact with the computers OS, so that you can accomplish, whatever you need to do.

bash is a simple language, which allows you to execute some commands in a programmable way.

bash --version

You can update the bash using the following command.

apt-get update && apt-get install bash bash3 bash-builtins bash-doc bash3-doc

Compound commands —

  • Grouping Commands in a subshell
  • Arithmetic Evaluation
  • The conditional Expression
  • The classic for-loop
  • The C-style for-loop
  • User Selection
  • The Case statement
  • The If-clause
  • The while loop
  • The until loop

Compound commands have the following characteristics:

  • They begin and end with a specific keyword or operator ( eg fordone)
  • They can be redirected as the whole.

Grouping Commands in a subshell (<list>) — The list is executed in a separate shell — a subprocess.

(cd opt/tools/VHostScan/; ls)

Grouping Commands {<list>} — The list command is simply executed in the current shell environment, the command must be terminated with newline or semicolon.

{ cd opt/tools/VHostScan/; ls; }

Arithmetic Evaluation (( <Expression> )) — This command evalauates the arithmetic expression.

In arithmetic evaluation 0 is FALSE and 1 is TRUE

In the above script, if you type 1 `Your choice is TRUE` will return and if you type 0 `Your choice is FALSE`

The conditional Expression [[<expression>]] — The conditional expression is meant for use as a conditional command like.

<expression1> && <expression2><expression1> || <expression2><expression1> == <expression2><expression1> = <expression2><expression1> != <expression2>

Example script —

The classic for-loop

for <name> in <words> ; do <list>; done

Getting Ip using dig and doing a whois on IP is taken from Dig.

for ip in `dig +short`; do whois $ip; done
for IP in `cat company_ip.txt`; do echo "$IP =>`dig -x $IP +short`";donefor IP in `cat company_ip.txt`; do echo "$IP =>`Any command you want to run`";donefor subdomain in `cat`; do echo "Working on $subdomain =>`ds $subdomain`";done

Creating and deleting multiple files.

for x in `seq 1 5`; do touch ehsahil_file_${x}.txt; done //creating 
for x in `seq 1 5`; do rm ehsahil_file_${x}.txt; done
for files in /media/* do echo $files done

Extracting multiple files in one command.

for i in $(ls *.tar.gz); do tar zxvf $i; done

The C-style for-loop

for ((<Expr1>;<expr2>;<expr3>;)); do <list> ;done

Example: —

for ((x=1; x<=3; x++))
echo $x

Simple Counter:

for ((x = 0 ; x <= 100 ; x++)); do
echo "Counter: $x"

Stepping counter

for ((x = 0 ; x <= 100 ; x += 10)); do
echo "Counter: $x"

User Selection

select <NAME> in <WORDS> ; do <LIST> ; doneselect x in 1 2 3
echo $x

The Case statement — The case-statement can execute commands based on a pattern matching decision

The If-clause — The if-clause can control the script's flow (what's executed) by looking at the exit codes of other commands.

The while loop — The while-loop is relatively simple in what it does: it executes the command list <LIST1> and if the exit code of it was 0 (TRUE) it executes <LIST2>. This happens again and again until <LIST1> returns FALSE.

while <LIST1> ; do

The until loop — The until-loop is relatively simple in what it does: it executes the command list <LIST1> and if the exit code of it was not 0 (FALSE) it executes <LIST2>. This happens again and again until <LIST1> returns TRUE.

until <LIST1> ; do

Running Bash Scripts

The script should have execute permission for the correct owner for running. we can change the permission using chmod

chmod u+x

Defining which shell to run,


We need to give the proper permission to the script before running it.

Understanding permissions within Unix.

bash is all you need.

Bash script

Convert domain list to resolved IP address list.

Checking for zone transfer.

#!/bin/bashfor i in $(cat domains.txt)do host -l $idone

Give proper permission.

chmod +x

Reverse lookup for each IP in a subnet.



echo "Please enter first 3 octets. e.g 192.168.1"
read subnet
while [ $i -le 254 ]; do
host -l "$subnet"."$i"
i=$(( $i + 1))


The more you practice bash and use it for day to day work, the more you will become good at it.

Nothing more to see here.

Yea !! — Part-2

Good read, If interested : —

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade
A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store