Skip to content
BENIBIX Learn

Shell Script

Development🔤 LanguagesPopular Languages

Also known as Bash Script, Bash Scripting, sh Script, .sh File

A file of commands the shell runs in turn, so you type less.

Think of it like

It is like a to-do list you give a helper. The helper does each line in order.

Example

Jack writes a three-line script that copies his notes folder into a dated backup. A timer runs it every night.

Why it matters

Most server and build jobs are glued together with shell scripts. They turn a list of steps into one command.

Definition

A shell script is a text file full of commands that a shell runs one after another. Anything you can type at the command line can go in it. The first line, called the shebang, names the shell to use, such as Bash. You make the file runnable once, then run it by name. Scripts save time on jobs you do again and again.

Example code

bash
#!/bin/bash
echo "Backing up notes"
tar -czf "notes-$(date +%F).tar.gz" ~/notes
The first line picks Bash. The last line packs the notes folder into a dated file.
Shell scripts sit on the shell and scripting shelf, by the prompt.

Common mistake

Leaving out quotes around names. A file name with a space then splits in two, and the script works on the wrong thing.

Key takeaways

  1. 1
    Commands in a file. Same as typing them.
  2. 2
    Shebang first. Names the shell to use.
  3. 3
    Run it again. One name for many steps.

Compare

Learn it properly