Shell scripting in Linux allows you to automate tasks, making system administration more efficient. By writing scripts, you can sequence commands, manage system operations, and create custom utilities tailored to your needs.
#!
) to define the shell interpreter. The most common shebang for Linux scripts is #!/bin/bash
, which specifies the Bash shell.Script File: Write your commands in a plain text file and save it with a .sh
extension.
#!/bin/bash
echo "This is a basic shell script."
chmod
command:
chmod +x script.sh
./script.sh
=
sign.$
before the variable name to access its value.name="Sumit"
echo "Hello, $name"
for
: Iterates over a list of items.while
: Repeats as long as a condition is true.until
: Repeats until a condition becomes true.for i in {1..5}; do
echo "Loop iteration: $i"
done
counter=1
while [ $counter -le 5 ]; do
echo "Counter: $counter"
((counter++))
done
if [ condition ]; then ... fi
structure is used to execute commands based on conditions.if [ "$name" == "Sumit" ]; then
echo "Welcome, Sumit!"
else
echo "You are not Sumit."
fi
&&
(and) or ||
(or).()
and enclosed in {}
.greet() {
echo "Hello, $1!"
}
greet "Sumit"
$1
, $2
, etc.#
) to describe what your script or specific sections do.By understanding these scripting basics, you can automate complex tasks, making your work on Linux systems more streamlined and powerful.
Go back to topics page [[Linux Basics]].