Shell Scripting - Detailed Notes
Creditation: Raksha
Group: Bughunters
Section: A
What is Shell Scripting?
Shell scripting is the practice of writing a sequence of Linux/Unix shell commands in a plain text file,
known as a script, which can be executed as a program.
Key Features:
- Automates manual command-line tasks
- Supports Variables, Loops, Conditionals, Functions
- Executes system-level commands
- Ideal for file ops, cron jobs, and automation
Popular Shell Interpreters:
- sh: Original Bourne shell
- bash: Bourne Again Shell (most used)
- zsh: Feature-rich, customizable shell
- ksh: Korn Shell
- csh: C-style Shell
Basic Structure of a Shell Script:
#!/bin/bash
# This is a comment
echo "Hello, world!"
1. Variables:
name="Raksha"
echo "Hello, $name"
2. User Input:
read user
echo "Welcome, $user"
3. Conditionals:
if [ "$user" == "Raksha" ]; then
echo "Welcome, Raksha!"
else
echo "Access Denied"
fi
4. Loops:
for i in 1 2 3 4
do
echo "Number: $i"
done
5. Functions:
greet() {
echo "Hello $1"
greet "Alice"
Script Execution:
chmod +x [Link]
./[Link]
Tips:
- Quote variables: "$var"
- Use set -e to exit on error
- Use #!/bin/bash -x for debugging
Example: Backup Script
#!/bin/bash
echo "Backing up files..."
tar -czvf [Link] /home/username/Documents
echo "Backup complete."