What is a shell script?
A shell script saves a sequence of terminal commands in a file so they run the same way every time. The shell, most often Bash (Bourne Again Shell), reads the file and runs each command in turn, with no compiling step. The .sh extension is a convention that tells people what the file is; Linux decides how to run a script from its first line and its permissions, not from the extension. macOS has used zsh as its default interactive shell since Catalina but still runs Bash and sh scripts.
Making a script run
- Shebang: the first line names the interpreter, for example
#!/bin/bash,#!/bin/shor#!/usr/bin/env bash. - Execute permission:
chmod +x backup.shmarks the file as executable. - Run it:
./backup.sh. The./is needed because the current directory is normally not in thePATH.
bash backup.sh runs a script without the execute bit. Be careful with sh backup.sh: on Debian and Ubuntu, sh is the smaller Dash shell, so Bash-only syntax can fail.
Building blocks
- Comments: lines that start with
#. - Variables:
DEST=/mnt/backup, with no spaces around=. Read it as$DEST, quoted as"$DEST"so spaces in a value do not break commands. - Command substitution:
STAMP=$(date +%F)stores today's date. - Arguments:
$1,$2and so on hold values passed on the command line, and$#is how many there are. - Exit status:
$?holds the last command's result; 0 means success. - Conditionals:
if [ -d "$DEST" ]; then ... else ... fitests whether a directory exists, and-ftests for a file. - Loops:
for host in web1 web2; do ping -c 1 "$host"; donerepeats a command for each item, andwhileloops repeat until a condition changes.
Put together, a small nightly backup script might read, line by line:
#!/bin/bashDEST=/mnt/backupif [ -d "$DEST" ]; then rsync -a /home/ "$DEST/home/"else echo "Backup drive not mounted"; exit 1; fi
Scheduled with cron, for example the line 0 2 * * * /home/admin/backup.sh added through crontab -e, it runs every night at 2:00 a.m. with no one signed in.
Typical uses
Technicians write shell scripts to automate repetitive work, restart machines (sudo reboot), remount network shares (mount), install software (sudo apt install -y), back up data (rsync or tar), gather inventory or log data, and start updates. The Windows counterparts are batch files (.bat) and PowerShell scripts (.ps1), while Python (.py) runs on all three platforms.
Risks and safe habits
- Untrusted scripts: a script from a forum or an email runs with your permissions. Piping a download straight into a shell, especially with sudo, runs code nobody has reviewed. Read it first.
- Unintended changes: one bad path can delete or overwrite data. If
DIRwas never set,rm -rf "$DIR"/*turns into an attempt to delete everything under/. Addingset -umakes a script stop when it uses an unset variable, andset -estops it at the first failing command. - Resource problems: a loop with no exit, or a script that writes logs without limit, can use all the CPU or fill a disk.
- Least privilege and testing: run as a normal user where possible, use sudo only for the commands that need it, and test in a virtual machine or on one system before rolling out.
- Windows line endings: a script saved with Windows CRLF line endings fails with an error mentioning
/bin/bash^M: bad interpreter. Convert it withdos2unix.
Shell script on the CompTIA A+ exam
Shell scripts fall under Core 2 (220-1202) objective 4.8, scripting fundamentals. Match .sh to Linux and macOS, .bat and .ps1 to Windows, .vbs to legacy Windows VBScript, .js to JavaScript and .py to Python. Recognize variables, conditionals and loops, the common automation uses, and the risks of running scripts you have not reviewed. If a question asks which file type automates a task on a Linux server, think .sh.