Skip to Content

Bash Basics

Bash script হলো একটা text file যেখানে terminal commands লেখা থাকে — একটার পর একটা। File-টা চালালে সব command ক্রমানুসারে execute হয়। Manual কাজ automate করার সবচেয়ে direct পথ।

Script কেন শিখবে

ধরো TryHackMe-তে একটা machine-এ 1 থেকে 254 পর্যন্ত সব IP-তে ping করতে হবে — কোনটা alive। হাতে হাতে ২৫৪টা command চালাবে?

Terminal
# Script ছাড়া: ২৫৪ বার এই command ping -c 1 192.168.1.1 ping -c 1 192.168.1.2 # ... ping -c 1 192.168.1.254 # Script দিয়ে: একটাই file চালাও ./ping_sweep.sh 192.168.1

এটাই scripting-এর শক্তি।

প্রথম Script

Terminal
# নতুন script file তৈরি করো nano hello.sh

Script-এর ভেতরে লেখো:

hello.sh
#!/bin/bash echo "Hello, Omar!" echo "আজকের তারিখ: $(date)"
Terminal
# Script-কে executable করো chmod +x hello.sh # চালাও ./hello.sh # Hello, Omar! # আজকের তারিখ: Sat Apr 11 17:00:00 UTC 2026

Shebang (#!/bin/bash)

প্রতিটা bash script-এর প্রথম line হলো shebang।

script.sh
#!/bin/bash

এই line Linux-কে বলে: “এই file-টা /bin/bash দিয়ে চালাও।” ছাড়া দিলে Linux জানবে না কোন interpreter ব্যবহার করবে।

Terminal
# bash কোথায় আছে দেখো which bash # /bin/bash

Variables

Variable হলো একটা নাম যেটার আড়ালে কোনো value রাখা হয়।

script.sh
#!/bin/bash # Variable তৈরি করো — = এর দুই পাশে space দেবে না name="Omar" target="10.10.10.50" port=80 # Variable ব্যবহার করো — $ দিয়ে echo "Name: $name" echo "Target: $target:$port"
Terminal
# Output: # Name: Omar # Target: 10.10.10.50:80

Command-এর output variable-এ রাখো:

script.sh
#!/bin/bash # $(...) দিয়ে command-এর output ধরো current_date=$(date) my_ip=$(hostname -I | awk '{print $1}') open_ports=$(ss -tlnp | grep LISTEN | wc -l) echo "Date: $current_date" echo "My IP: $my_ip" echo "Open ports: $open_ports"

User Input — read

Script চলার সময় user-এর কাছ থেকে input নাও।

script.sh
#!/bin/bash echo "Target IP দাও:" read target_ip echo "Port দাও:" read port echo "Scanning $target_ip:$port..." nc -zv $target_ip $port
Terminal
# Prompt একই line-এ রাখতে read -p "Target IP: " target_ip read -p "Port: " port

Conditions — if

কোনো condition সত্য হলে কিছু করো।

script.sh
#!/bin/bash read -p "Port number দাও: " port if [ $port -lt 1024 ]; then echo "Well-known port — system service" elif [ $port -lt 49152 ]; then echo "Registered port — application" else echo "Dynamic/Private port" fi

Comparison operators:

NumericStringমানে
-eq=সমান
-ne!=সমান নয়
-ltছোট
-gtবড়
-leছোট বা সমান
-geবড় বা সমান

File check:

script.sh
#!/bin/bash if [ -f "/etc/passwd" ]; then echo "File আছে" fi if [ -d "/tmp" ]; then echo "Directory আছে" fi if [ -x "/usr/bin/nmap" ]; then echo "nmap installed এবং executable" fi
Flagমানে
-fFile আছে
-dDirectory আছে
-xExecutable
-eExists (file বা dir)
-zEmpty string

Loops

for loop — নির্দিষ্ট সংখ্যকবার চালাও:

script.sh
#!/bin/bash # একটা list-এর প্রতিটা item for user in root omar the-queen www-data; do echo "Checking user: $user" done # Number range for i in {1..10}; do echo "Count: $i" done # IP range (ping sweep) for ip in {1..254}; do ping -c 1 -W 1 192.168.1.$ip &>/dev/null && echo "192.168.1.$ip is alive" done

while loop — condition সত্য থাকলে চালাও:

script.sh
#!/bin/bash count=1 while [ $count -le 5 ]; do echo "Attempt $count" count=$((count + 1)) done

File-এর প্রতিটা line read করো:

script.sh
#!/bin/bash # wordlist.txt-এর প্রতিটা word দিয়ে কিছু করো while read -r word; do echo "Testing: $word" done < wordlist.txt

Arguments

Script চালানোর সময় বাইরে থেকে value দাও।

script.sh
#!/bin/bash # $1 = প্রথম argument, $2 = দ্বিতীয়, $@ = সব echo "Script নাম: $0" echo "Target: $1" echo "Port: $2" echo "সব arguments: $@" echo "মোট arguments: $#"
Terminal
./script.sh 10.10.10.50 80 # Script নাম: ./script.sh # Target: 10.10.10.50 # Port: 80

Argument validation:

script.sh
#!/bin/bash if [ $# -eq 0 ]; then echo "Usage: $0 <target-ip>" exit 1 fi target=$1 echo "Scanning $target..."

Output Colors

Terminal output-কে color করো — পড়তে সহজ হয়।

script.sh
#!/bin/bash # Color codes RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color — reset echo -e "${GREEN}[+] Success${NC}" echo -e "${RED}[-] Failed${NC}" echo -e "${YELLOW}[*] Info${NC}"

CyberSec Note

Bash script — pentesting এবং CTF-এ:

Script-এ sensitive info রেখো না:

script.sh
# ভুল — password hardcode করা password="secret123" # ঠিক — runtime-এ নাও read -sp "Password: " password # -s = silent (typing দেখাবে না)

Target machine-এ script দেখলে বুঝবে কী:

Terminal
# .sh files খোঁজো find / -name "*.sh" 2>/dev/null # Cron job-এ কোন script চলছে (scheduled tasks) cat /etc/crontab ls /etc/cron.d/ crontab -l # যদি script world-writable হয় এবং root এটা চালায় # সেটা privilege escalation এর সুযোগ ls -la /etc/cron.d/backup.sh # -rwxrwxrwx → যে কেউ edit করতে পারে → root হিসেবে চলবে

Quick Check

  • Shebang কী এবং কেন দরকার?
  • Variable-এ value রাখতে = এর দুই পাশে space দিলে কী হবে?
  • $1 মানে কী?
  • if [ -f "/etc/passwd" ] — এটা কী check করে?
  • Script-কে চালানোর আগে কোন command দিতে হয়?

পরবর্তী → Automation Scripts

Search Keywords: bash scripting bangla, bash variables conditions loops Bengali, bash script tutorial, shebang bash, if else bash, for loop bash, bash arguments

Last updated on