What you'll build
You'll create your first Bash script: a file that executes multiple commands automatically. You'll write a simple script that shows system information, and then a more useful one that creates projects with ready structure. When you're done, you'll have reusable scripts you can run with a single command to automate repetitive tasks. It's the foundation for creating your own tools, automating backups, deployments, and any terminal task.
Step 1: Create your first script
# Create the file
touch my-script.sh
# Open in your editor
code my-script.sh # or nano, vim, etc.
Step 2: Write the script
#!/bin/bash
# My first script
echo "Hello, starting tasks..."
echo "Current date: $(date)"
echo "You are in: $(pwd)"
echo "Files here:"
ls -la
echo "Done!"
Step 3: Make it executable and run it
chmod +x my-script.sh
./my-script.sh
Practical example: Project setup
#!/bin/bash
# setup-project.sh
PROJECT_NAME=$1 # First argument
if [ -z "$PROJECT_NAME" ]; then
echo "Usage: ./setup-project.sh project-name"
exit 1
fi
mkdir -p "$PROJECT_NAME"
cd "$PROJECT_NAME"
git init
echo "# $PROJECT_NAME" > README.md
echo "node_modules/" > .gitignore
npm init -y
echo "Project $PROJECT_NAME created!"
Usage: ./setup-project.sh my-app
Variables and conditionals
#!/bin/bash
NAME="User"
if [ -f "config.json" ]; then
echo "Config found"
else
echo "Config doesn't exist, creating..."
echo "{}" > config.json
fi
Loops
#!/bin/bash
# Loop over files
for file in *.txt; do
echo "Processing: $file"
done
# Loop with counter
for i in {1..5}; do
echo "Iteration $i"
done
If something failed
| Error | Cause | Solution |
|---|---|---|
Permission denied | Not executable | chmod +x script.sh |
command not found | First line wrong | Add #!/bin/bash |
syntax error | Syntax error | Check spaces in if [ ] |
Next step
โ Docker Hello World โ Basic containers