Back to programming
programming#bash#shell#linux

Bash Scripting: The Basics You'll Use Every Week

Variables, loops, conditionals and pipes in Bash — enough to automate 90% of everyday tasks.

Jane Contributor August 2, 2026 1 views

Bash Basics

Bash is the default shell on Linux/macOS. Learn a little and you'll automate hours of clicking.

Your first script

hello.sh:

#!/usr/bin/env bash
name=${1:-World}       # first argument, or "World" if missing
echo "Hello, $name!"
chmod +x hello.sh
./hello.sh Priya       # Hello, Priya!

Variables

count=5                # NO spaces around =
echo "count is $count"

Conditionals

if [[ $count -gt 3 ]]; then
  echo "big"
elif [[ $count -eq 3 ]]; then
  echo "medium"
else
  echo "small"
fi

Loops

for f in *.log; do
  gzip "$f"
done

i=0
while (( i < 3 )); do
  echo $i
  ((i++))
done

Pipes and redirects

ls -la | grep '.py$' | wc -l          # count Python files
python script.py > out.log 2>&1       # capture stdout + stderr

Real-world example

Nightly backup:

#!/usr/bin/env bash
DATE=$(date +%F)
tar -czf "/backups/notes-$DATE.tgz" /var/notes
find /backups -name 'notes-*.tgz' -mtime +14 -delete

Beginner tip

Always quote variables ("$name"), always run shellcheck script.sh — it catches 95% of bash bugs.

Keep reading

You may also like

Discussion (0)

No comments yet. Be the first to weigh in.

Leave a comment

Comments are reviewed before appearing.