00. Linux Shells and Shell Scripting
What is a Shell?¶
A shell is a command-line interface (CLI) that allows users to interact with the operating system. It acts as a bridge between the user and the kernel, interpreting commands and executing them.
- User enters command → Shell interprets → Kernel executes → Output returned
Check Current Shell¶
Example Output¶
Explanation¶
-
$SHELLis an environment variable. -
It stores the path of the current default shell.
List All Available Shells¶
Example Output¶
Explanation¶
-
This file contains all shells installed on the system.
-
Useful when changing your default shell.
Switch Shell (Temporary)¶
Explanation¶
-
Starts a new shell session (Zsh here).
-
Does not change default shell.
To return:
Change Default Shell (Permanent)¶
Explanation¶
-
chsh= change shell -
-sspecifies the shell path -
Takes effect after logout/login
Popular Shells Comparison¶
| Feature | Bash | Zsh | Fish |
|---|---|---|---|
| Default in Linux | Yes | No | No |
| Scripting | Strong | Strong | Limited |
| Auto-completion | Basic | Advanced | Very advanced |
| Customization | Moderate | Very high | High |
| Plugins | Limited | Extensive (Oh My Zsh) | Built-in features |
What is a Shell Script?¶
A shell script is a file containing a sequence of commands executed by the shell.
-
File extension:
.sh -
Used for automation, system tasks, and scripting workflows
Creating and Running a Script¶
Step 1: Create File¶
Step 2: Add Code¶
Explanation¶
-
#!/bin/bash→ Shebang (defines interpreter) -
echo→ Prints output
Step 3: Make Executable¶
Explanation¶
- Adds execute permission
Step 4: Run Script¶
Variables in Shell¶
Explanation¶
-
read name→ takes user input -
$name→ accesses variable value
Loops¶
For Loop¶
Explanation¶
-
{1..5}→ sequence generator -
Loop runs 5 times
-
$i→ current iteration value
While Loop¶
Explanation¶
-
Runs while condition is true
-
-lemeans "less than or equal" -
((count++))increments value
Conditional Statements¶
#!/bin/bash
echo "Enter username:"
read user
if [ "$user" = "admin" ]; then
echo "Access granted"
else
echo "Access denied"
fi
Explanation¶
-
if [ condition ]→ evaluates condition -
=→ string comparison -
then→ executes if true -
else→ executes if false -
fi→ ends block
Command Line Arguments¶
Run:¶
Output:¶
Explanation¶
-
$0→ script name -
$1, $2→ arguments passed
Functions in Shell¶
Explanation¶
-
greet()defines function -
$1inside function → argument
Comments¶
Best Practice¶
-
Use comments to explain logic
-
Improve readability and maintainability
File Operations Example¶
#!/bin/bash
file="test.txt"
if [ -f "$file" ]; then
echo "File exists"
else
echo "File not found"
fi
Explanation¶
-
-f→ checks if file exists -
Useful in automation scripts
Exit Status¶
Explanation¶
-
$?→ last command status -
0→ success -
non-zero → error
Best Practices¶
-
Always use shebang (
#!/bin/bash) -
Use meaningful variable names
-
Quote variables:
"$var" -
Add comments for complex logic
-
Test scripts before deployment
Summary¶
| Concept | Description |
|---|---|
| Shell | Interface between user and OS |
| Script | File with executable commands |
| Variables | Store values |
| Loops | Repeat execution |
| Conditionals | Decision making |
| Functions | Reusable blocks |
| Permissions | Required to execute scripts |