Variables are assigned with = (no spaces). Reference with $. # starts a comment. Use echo or printf for output.
#!/bin/bash name="Alice" echo "Hello, $name!" # double quotes expand vars echo 'Hello, $name!' # single quotes are literal read -p "Enter: " input # read user input x=42 echo $((x + 8)) # arithmetic: 50
# if / elif / else
if [[ $x -gt 10 ]]; then
echo "big"
elif [[ $x -gt 0 ]]; then
echo "small"
else
echo "zero or negative"
fi
# for loop
for i in 1 2 3 4 5; do echo $i; done
for i in $(seq 1 10); do echo $i; done
for f in *.txt; do echo "$f"; done
# while loop
while [[ $x -gt 0 ]]; do
echo $x
((x--))
done
# case
case "$input" in
start) echo "Starting...";;
stop) echo "Stopping...";;
*) echo "Unknown";;
esac
# Define and call functions
greet() {
local name="$1"
echo "Hello, $name!"
}
greet "Alice" # Hello, Alice!
add() {
echo $(($1 + $2))
}
result=$(add 5 3) # capture output: 8
# $@ = all arguments, $# = argument count
# $? = exit status of last command
# $0 = script name
Core text tools: awk, sed, tr, cut, sort, uniq, grep.
# awk - field processing
echo "Alice 30 NY" | awk '{print $1, $3}' # Alice NY
awk -F',' '{print $2}' data.csv # 2nd CSV field
# sed - stream editor
sed 's/old/new/g' file.txt # replace all
sed -i 's/foo/bar/' file.txt # in-place edit
# tr - translate characters
echo "hello" | tr 'a-z' 'A-Z' # HELLO
echo "hello" | tr -d 'aeiou' # hll
# cut - extract fields
echo "a,b,c" | cut -d',' -f2 # b
# sort + uniq
sort file.txt | uniq -c # count unique lines
sort -n numbers.txt # numeric sort
# grep
grep -i "error" *.log # case-insensitive
grep -c "pattern" file.txt # count matches
grep -r "TODO" . # recursive search
# Indexed arrays
arr=(apple banana cherry)
echo ${arr[0]} # apple
echo ${arr[@]} # all elements
echo ${#arr[@]} # length: 3
# Associative arrays (bash 4+)
declare -A map
map[name]="Alice"
map[age]=30
echo ${map[name]} # Alice
# Sequence & shuffle
seq 1 5 # 1 2 3 4 5
shuf -e a b c d # random order
# Read into array
mapfile -t lines < file.txt
# jq for JSON
echo '{"name":"Alice"}' | jq '.name' # "Alice"
curl -s api.example.com | jq '.[0]'
# File tests
[[ -f "file.txt" ]] # exists and is file
[[ -d "dir" ]] # exists and is directory
[[ -s "file.txt" ]] # exists and not empty
# Read file line by line
while IFS= read -r line; do
echo "$line"
done < file.txt
# File info
stat file.txt # metadata
basename "/path/to/file.txt" # file.txt
dirname "/path/to/file.txt" # /path/to
wc -l file.txt # line count
# Processes
ps aux # all processes
kill -9 <PID> # force kill
pgrep -f "pattern" # find by name
# Connectivity ping -c 3 google.com curl -s https://api.example.com wget -q -O file.html https://example.com # Port checks nc -zv host 80 # TCP port test ss -tuln # listening sockets netstat -tulnp # legacy alternative # DNS dig example.com nslookup example.com host example.com
# Safe mode - stop on errors set -euo pipefail # Cleanup on exit trap 'rm -f /tmp/mytemp' EXIT # Quote all variable expansions echo "$variable" # correct echo $variable # risky with spaces # Forward arguments safely my_func "$@" # Check command exists command -v curl &>/dev/null || echo "curl not found" # Timeout long commands timeout 5s ping google.com
$? # exit status of last command (0=success) $@ # all arguments (preserves quoting) $# # number of arguments $0 # script name $$ # current process ID !! # re-run last command (interactive) # Redirection cmd > out.txt # stdout to file cmd 2> err.txt # stderr to file cmd &> all.txt # both to file cmd1 | cmd2 # pipe stdout # Logical operators cmd1 && cmd2 # run cmd2 if cmd1 succeeds cmd1 || cmd2 # run cmd2 if cmd1 fails
Try Bash commands interactively. This simulator supports common commands like echo, printf, variable assignment, arithmetic $(( )), pipes, tr, cut, awk, sed, grep, sort, uniq, wc, rev, seq, head, tail, and more.