From the Linux Terminal to Shell Scripts
Once you are comfortable typing commands, shell scripts let you automate backups, deployments, and file chores. Bash is the default shell on Ubuntu — scripts are plain text files executed by the terminal.
Your first script
#!/bin/bash
echo "Hello from $(hostname)"
date >> ~/script-log.txt
Save as hello.sh, run chmod +x hello.sh, then ./hello.sh. The #!/bin/bash line tells Linux which interpreter to use.
Variables and input
NAME="Dave"
echo "Backup starting for $NAME"
read -p "Continue? (y/n) " ANSWER
Conditionals and loops
if [ -f /etc/hosts ]; then
cp /etc/hosts ~/hosts.backup
fi
for file in *.log; do
gzip "$file"
done
Best practices
- Use
set -eto exit on errors in production scripts. - Quote variables:
"$VAR"handles spaces safely. - Test with
shellcheck script.shafter installing ShellCheck via APT. - Schedule recurring jobs with
cronor systemd timers.
Scripts bridge manual terminal work and full applications. Start small — automate one annoying task this week, then combine scripts into larger workflows as you grow.
