Jul 20, 2024
echo $0
to check the default shell in Linux terminal.cat /etc/shells
to list all installed shells.Hello World
script.
#!/bin/bash
at the start (shebang line).echo "Hello World"
.chmod +x script_name
to make it executable../script_name
.#
at the start of the line.: ' your comment here ' :
.variable_name=value
(No spaces around =
).$variable_name
.name="Prashant"
echo $name
array_name=(value1 value2 value3)
.${array_name[index]}
.fruits=("Apple" "Banana" "Cherry")
echo ${fruits[1]} # Outputs: Banana
${array_name[@]}
.${#array_name[@]}
.${array_name[@]:start:length}
.array_name+=("AnotherValue")
.${#var}
.${var^^}
.${var,,}
.${var:position:length}
.read variable_name
.echo "Enter your name:"
read name
echo "Hello, $name!"
$((expression))
for performing arithmetic operations.a=10
b=20
sum=$((a + b))
echo $sum # Outputs: 30
if [ condition ]; then
# Commands
elif [ condition ]; then
# Commands
else
# Commands
fi
if [ $a -gt $b ]; then
echo "$a is greater than $b"
else
echo "$a is not greater than $b"
fi
-eq
: Equal-ne
: Not equal-gt
: Greater than-ge
: Greater than or equal to-lt
: Less than-le
: Less than or equal to&&
or -a
||
or -o
for var in list; do
# Commands
done
while [ condition ]; do
# Commands
done
until [ condition ]; do
# Commands
done
for i in {1..5}; do
echo "Number $i"
done
function_name() {
# Commands
}
greet() {
echo "Hello, $1"
}
greet "World" # Outputs: Hello, World
$1
, $2
, etc., within the function.return
. Capture using $?
.free
, awk
, if-else
structure, mail
command for alert.df
, find
, tar
, and mail
.find
, gzip
, and mv
.* * * * * command-to-execute
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | -------- Day of month (1 - 31)
| ---------- Hour (0 - 23)
------------ Minute (0 - 59)
0 5 * * * /path/to/script
(Runs script daily at 5 AM).