Shell Scripting Basics Cheat Sheet
#!/bin/bash
echo "Hello, World!"
my_var="Hello"
echo $my_var
if [ condition ]; then
# Commands if condition is true
echo "Condition is true"
fi
if [ condition ]; then
echo "Condition is true"
else
echo "Condition is false"
fi
for i in {1..5}; do
echo "Iteration $i"
done
count=1
while [ $count -le 5 ]; do
echo "Count: $count"
((count++))
done
read -p "Enter your name: " name
echo "Hello, $name!"
my_function() {
echo "This is a function!"
}
my_function
# Access arguments with $1, $2, etc.
echo "First argument: $1"
echo "Second argument: $2"
if [ -f "filename.txt" ]; then
echo "File exists"
else
echo "File does not exist"
fi
if [ condition1 ] && [ condition2 ]; then
echo "Both conditions are true"
fi
if [ condition1 ] || [ condition2 ]; then
echo "At least one condition is true"
fi
case "$variable" in
"value1") echo "Value is 1";;
"value2") echo "Value is 2";;
*) echo "Default case";;
esac
exit 0
echo "Output to file" > output.txt
echo "Append to file" >> output.txt
command1 && command2 # Executes command2 only if command1 succeeds
command1 || command2 # Executes command2 only if command1 fails