Bash for Everyone — Part 1

I’ve divided this post 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 

  • Pipelines
  • Bash – Bourne Again Shell
  • Compound Statements
  • Running bash scripts
  • File permissions


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.

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

Shell Startup — Sh/Bash/zsh | Source —

Valid Login shell for my machine —

cat /etc/shells
screenshot 2019-01-04 at 7.36.07 am
Type of shell
  • /bin/bash – Bourne again shell
  • /bin/csh – C shell
  • /bin/ksh – Korn shell
  • /bin/sh – Bourne Shell, The orginal 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
screenshot 2019-01-04 at 7.45.32 am

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 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 for … done)
  • 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

read mychoice
if ((mychoice));then
echo "Your choice is TRUE"
echo "Your choice is FALSE"

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 —

echo "Enter DNS Service you like>"
read input
if [ "$cloudflare_dns_service" == "$user_dns_service" ];then
echo "This is Cloudflare DNS service :) "
echo "This is NOT Cloudflare DNS service :("

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

screenshot 2019-01-04 at 8.01.55 am

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 //deleting
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> ; done
select x in 1 2 3
  echo $x

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

printf '%s ' 'Which tool you want to run for finding subdomains?'
read -${BASH_VERSION+e}r subdomain
case $subdomain in
subfinder -d
python -d
echo "This tool is not available"

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

echo "Enter DNS Service you like>"
read input
if [ "$user_dns_service" == "$cloudflare_dns_service" ];then
echo "This is Cloudflare DNS service :)"
elif [ "$user_dns_service" == "$google_dns_service" ];then
echo "This is Google DNS Service :)"
echo "This is  Not Cloudflare or Google DNS service :("

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 script

Convert domain list to resolved IP address list. by @xdavidhu
# This is a script inspired by the Bug Hunter's Methodology 3 by @Jhaddix
# With this script, you can convert domain lists to resolved IP lists without duplicates.
# Usage: ./ [domain-list-file] [output-file]
echo -e "[+] by @xdavidhu\n"
if [ -z "$1" ] || [ -z "$2" ]; then
  echo "[!] Usage: ./ [domain-list-file] [output-file]"
  exit 1
echo "[+] Resolving domains to IPs..."
while read d || [[ -n $d ]]; do
  ip=$(dig +short $d|grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b"|head -1)
  if [ -n "$ip" ]; then
    echo "[+] '$d' => $ip"
    echo $ip >> $2
    echo "[!] '$d' => [RESOLVE ERROR]"
done < $1
echo -e "\n[+] Removing duplicates..."
sort $2 | uniq > $
mv $ $2
echo -e "\n[+] Done, IPs saved to '$2'."

Checking for zone transfer.

for i in $(cat domains.txt)
do host -l $i

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


Ten Things I Wish I’d Known About bash




Leave a Reply