🔹 Basic Syntax
#!/bin/bash
— Shebang to define the shell interpreter#
— Single-line commentecho "Hello"
— Print outputread name
— Accept user input$(command)
or\`command\`
— Command substitution
🔄 Conditionals & Loops
if [ $a -eq $b ]; then ... fi
— Basic if conditionelif
andelse
— Additional condition checkswhile [ condition ]; do ... done
— While loopfor i in {1..5}; do ... done
— For loopcase $var in ...) esac
— Case/switch logic
🧠 Variables & Parameters
var=value
— Assign variable (no space before/after =)$var
— Access variable$1, $2, ...
— Script arguments$#
— Number of arguments$?
— Exit status of last command
📁 File & Directory Handling
[ -f file.txt ]
— Check if file exists[ -d dir ]
— Check if directory existstouch, mkdir, rm, mv
— File operationscat, head, tail, grep
— Read/search files
🔧 Practical Example
#!/bin/bash
echo "Enter a filename:"
read fname
if [ -f "$fname" ]; then
echo "Contents of $fname:"
cat "$fname"
else
echo "$fname does not exist!"
fi