October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Write a Simple Bash Script: A Beginner’s Guide

Create a simple Bash script, make it executable, and build toward a reusable directory-backup task with variables, arguments, and interactive input.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A Bash script is a plain-text file containing commands for Bash to run. To make one, put #!/bin/bash on its first line, add a command, save the file, and run it with the appropriate permissions. This guide builds from a one-command script to a reusable directory-backup example, including arguments and interactive input.

Create and run your first Bash script

  1. Open a plain-text editor and create a file named hello.sh. Use a text editor rather than a word processor, which may add formatting.

  2. Add a shebang on the first line and a comment describing the task:

    #!/bin/bash
    # Print a greeting
    
    echo "Hello, world!"

    The #! line is called a shebang. When you execute the file directly, it tells the system to use /bin/bash as the interpreter. This path is appropriate on many Linux systems; if Bash is installed elsewhere, use its actual path.

    Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  3. Save the file, then grant its owner read, write, and execute permissions:

    chmod 700 hello.sh

    With mode 700, the owner can read, write, and execute the file; group members and other users have no permissions. Use this when the script should be private to its owner. To check the mode and file details, run:

    ls -lh hello.sh
  4. Run it from the directory containing the file:

    ./hello.sh

    The expected output is Hello, world!. The ./ tells the shell to run the file in the current directory.

Put a useful command in the script

Once the basic structure is clear, replace the greeting with a small task you understand. For example, this script synchronizes one directory to another:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#!/bin/bash
# Copy a directory to a backup location

rsync -av "$HOME/Documents/" "$HOME/backup/Documents/"

rsync copies files and directories; the -a option enables archive behavior and -v prints the files being processed. The trailing slash on the source path means to copy the contents of Documents into the destination directory. Before relying on a backup script, confirm that the source and destination paths are correct and that you have permission to write to the destination.

Use variables for paths you may change

If a path appears multiple times or is likely to change, store it once in a variable. For example:

#!/bin/bash
# Copy a source directory to a backup destination

SOURCEDIR="$HOME/Documents/"
DESTDIR="$HOME/backup/Documents/"

rsync -av "$SOURCEDIR" "$DESTDIR"

Now you can update a path in one place rather than searching through several commands. The quotation marks keep paths containing spaces together as one argument.

Accept a path as a command-line argument

$1 represents the first argument supplied after the script name. This example accepts a source directory and uses a fixed destination:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#!/bin/bash
# Back up the directory supplied as the first argument

SOURCEDIR="$1"
DESTDIR="$HOME/backup/"

if [ -z "$SOURCEDIR" ]; then
  echo "Usage: $0 SOURCE_DIRECTORY" >&2
  exit 1
fi

rsync -av "$SOURCEDIR" "$DESTDIR"

Run it by supplying the source path:

./backup.sh "$HOME/Documents/"

Here, $0 refers to the script name, and the check prevents the command from running when no source argument is provided. Quoting the argument protects paths that contain spaces. The script exits with 1 for the missing-argument error; a successful command normally returns 0, while a failure returns a non-zero status.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Ask for input while the script runs

For an interactive script, use read to collect a value from the person running it:

#!/bin/bash
# Ask for a name and greet the user

printf "What is your name? "
read name
printf "Hello, %sn" "$name"

The script pauses at read name until the user enters input. Use a command-line argument instead when the script should run unattended or be called by another program.

Use exit statuses to report success or failure

Shell commands return an exit status: zero conventionally means success, and a non-zero value indicates failure. Administrative scripts should preserve that distinction so users and other programs can tell whether a task completed. Linux Standard Base guidance for init scripts follows this success/failure convention. For a script that runs a single command last, its exit status is typically the script’s status as well; scripts with more steps should check important commands and choose an appropriate status deliberately.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose what to learn next

For a next step, choose material based on whether you need a reference or guided practice, and whether it covers the Bash features you plan to use. The Linux Foundation’s Bash credential description lists simple commands, basic syntax, builtins, variables, special parameters, and environment concepts. A reference such as the Advanced Bash-Scripting Guide can suit readers ready to explore beyond a first exercise; structured training is a better fit for those who want a guided course. No learning-speed or productivity figures are established for these options.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.