#!/bin/bashTells the system to run the script using the Bash shell.
# This is a commentEverything after # is ignored by the shell.
name="John"
echo "Hello, $name"- No space around
=. - Use
$nameto access the value.
x=5
y=3
sum=$((x + y))
echo "Sum: $sum"Use $(( )) for arithmetic operations.
if [ "$name" == "John" ]; then
echo "Hello John"
else
echo "You're not John"
fi==,!=for strings-eq,-ne,-gt,-lt,-ge,-lefor numbers
for i in 1 2 3; do
echo "Number $i"
donecount=1
while [ $count -le 3 ]; do
echo "Count is $count"
((count++))
donegreet() {
echo "Hello, $1"
}
greet "Alice"$1,$2, etc., are positional arguments.
today=$(date)
echo "Today is $today"- Runs a command and stores the output in a variable.
if [ -f "file.txt" ]; then
echo "file.txt exists"
fi
if [ -d "/etc" ]; then
echo "/etc is a directory"
fiecho "First arg: $1"
echo "Second arg: $2"Call with:
./myscript.sh hello worldexit 0 # Success
exit 1 # Error| Syntax | Meaning |
|---|---|
$(command) |
Run command and get output |
$? |
Exit code of last command |
$# |
Number of arguments |
$@ or $* |
All arguments |