Shell scripting is writing a series of commands for the shell (command-line interpreter) to execute automatically.
It helps automate repetitive tasks, manage system operations, and simplify workflows.
Bash:
The most commonly used Linux shell (Bourne Again SHell).
Zsh:
An extended version of Bash with additional features.
Sh:
The original Bourne shell.
Shebang (
#!):
The first line in a shell script that tells the system which interpreter to use.
Example:#!/bin/bash
Comments:
Anything following#is a comment and is ignored by the shell.
Example:# This is a comment
Variables:
Store values in a script.
Example:NAME="Mukund" echo "Hello, $NAME"
Conditional Statements:
Perform different actions based on conditions.
Example:if [ "$NAME" == "Mukund" ]; then echo "Welcome Mukund!" else echo "Who are you?" fi
Loops:
Repeat a block of code multiple times.
Example:for i in 1 2 3 do echo "Number $i" done
Functions:
Group commands into reusable blocks.
Example:greet() { echo "Hello, Mukund!" } greet
-
Create a file with
.shextension.
Example:script.sh -
Make the script executable:
chmod +x script.sh