- Introduction
- Hello World
- Variables
- User Input
- Positional Arguments
- If-Else Statements
- Switch Case
- Functions
- Piping & Redirection
- Exit Codes
- AWK Basics
- SED (Stream Editor) Basics
This repository contains Bash scripting examples for automation, cybersecurity, and system administration.
#!/usr/bin/bash
echo "Hello World!"- Run the script:
chmod u+x hello.sh ./hello.sh
#!/usr/bin/bash
First_Name="Pratham"
Last_Name="Chauhan"
echo "Hello $First_Name $Last_Name"#!/usr/bin/bash
echo -n "Enter your name: "
read name
echo "Hello, $name!"#!/usr/bin/bash
echo "Hello $1 $2"- Run:
Output:
./script.sh Pratham Chauhan
Hello Pratham Chauhan
#!/usr/bin/bash
if [ "${1,,}" = "pratham" ]; then
echo "Welcome Back Boss"
elif [ "${1,,}" = "help" ]; then
echo "Enter your username"
else
echo "You are not my boss"
fi#!/usr/bin/bash
case "${1,,}" in
pratham | administrator)
echo "Hello boss"
;;
help)
echo "Enter your username"
;;
*)
echo "You are not the boss"
;;
esac#!/usr/bin/bash
showname(){
echo "Hello $1"
}
showname Prathamls -l /usr/bin | grep zshecho "Hello Pratham" > hello.txt # Overwrites file
echo "Hello Chauhan" >> hello.txt # Appends to file [ 200 -eq 100 ]
echo $? # Returns 1 (false)
[ 200 -eq 200 ]
echo $? # Returns 0 (true) awk '{print $1}' testfile.txt # Prints first word from each line
awk '{print $2}' testfile.txt # Prints second word from each line SED (Stream Editor) is a powerful tool for text manipulation.
sed 's/Pratham/He/g' sedtest.txtHello everyone my name is Anonymous. I am learning bash now.
Pratham is a good boy. Pratham can do everything.
Thank you that's from Pratham's side.
Hello everyone my name is Anonymous. I am learning bash now.
He is a good boy. He can do everything.
Thank you that's from He's side.
sed '3d' file.txtsed '/error/d' logfile.txtsed -i 's/oldtext/newtext/g' filename.txtsed -n '2,4p' filename.txtsed '/^$/d' file.txt- Clone the repository:
git clone https://github.com/yourusername/bash-scripting.git cd bash-scripting - Make scripts executable:
chmod u+x script.sh
- Run scripts:
./script.sh
Feel free to contribute by adding more examples or improving existing ones.
🚀 Happy Scripting! 🚀