What is Bash? Shell basics and a first script

Author: Published

In brief: Bash is a system shell: a program that reads user commands and arranges for them to be executed. It lets you work with files, run programs, and automate repetitive tasks using scripts. It is widely used on Linux and is also available on macOS. On Windows, you can use it through tools such as WSL or Git Bash.

What is Bash used for?

The name Bash stands for Bourne Again SHell, a reference to the earlier Bourne shell and a play on “born again.” Bash is both an interactive tool for issuing commands and a scripting language. A script is a text file containing instructions executed by an interpreter—in this case, Bash.

In interactive mode, you type a command and immediately get a result. In a script, you save a sequence of actions that you can run again later without manually repeating each step.

Bash is useful for tasks such as:

For example, rather than manually moving dozens of reports to the appropriate directory, you can express that operation in a single command. When the task also involves checking conditions and writing messages, a script becomes more convenient.

Bash, terminals, and commands are different things

A terminal is an interface where you type text and read responses. On a personal computer, it is usually a terminal emulator application. A shell runs within that session and interprets the instructions you enter. Bash is one shell; others include Zsh, Fish, and PowerShell.

Opening a terminal therefore does not necessarily mean you are using Bash. On newer macOS installations, the default shell is usually Zsh, while a terminal on Windows might open PowerShell or a Linux environment, for example.

Not every command used in Bash is part of Bash itself. cd, which changes directories, is a built-in command. By contrast, ls, grep, and mkdir are usually separate programs launched by the shell. The availability of these tools and the options they support depend on the environment.

Bash can connect programs using a pipe, represented by |. A pipe passes the standard output of one command to the standard input of the next:

printf '%s\n' 'report.txt' 'photo.jpg' | grep '\.txt$'

Here, printf prints two names, and grep selects the line ending in .txt. This only filters text—the example neither creates nor searches for files on disk.

How to check whether Bash is available and start it

On Linux, Bash is often already installed. macOS also provides Bash, although the supplied version may differ from those available in other environments. To check whether the program is available and see its version, enter this in the terminal:

bash --version

This check does not confirm that your current session is using Bash—it simply runs the available program with the option to display its version. The command bash starts a Bash session, and exit ends it. Once you are in Bash, you can check the current shell’s version using printf '%s\n' "$BASH_VERSION".

On Windows, two popular options serve different purposes. WSL, or Windows Subsystem for Linux, lets you use a Linux environment. Git Bash provides Bash and a set of tools useful for tasks such as working with Git, but it is not a complete Linux environment. Check the documentation for Microsoft or Git for Windows for current installation requirements.

Your first script in a safe test directory

The following example creates two empty reports and moves them to a subdirectory. Start by creating a new working directory and entering it:

mkdir bash-test
cd bash-test

If the directory already exists, choose a different name. Continue only after you have successfully entered the new directory. Using a text editor, create a file named organize.sh inside it with the following contents:

#!/usr/bin/env bash

directory="reports"
mkdir -p "$directory" || exit 1
touch report-01.txt report-02.txt || exit 1

for file in report-*.txt; do
  mv -- "$file" "$directory/" || exit 1
done

printf 'Moved reports to directory: %s\n' "$directory"

The first line, called the shebang, specifies the interpreter to use when the file is run directly. The variable directory holds the destination directory’s name, and the for loop repeats the operation for each matching filename. The expression || exit 1 stops the script if the preceding command reports an error.

Run the script and check the result:

bash organize.sh
ls reports

When you run the file this way, it does not need execute permission: you specify the interpreter with the bash command. Quoting variables prevents names containing spaces, among other things, from being split into separate arguments.

When to use Bash and what to watch out for

Bash is a good choice when a task mainly involves connecting existing tools and working with files. For extensive logic, complex data structures, or a larger application, Python or another general-purpose language will often be easier to read and test.

Commands run with the permissions of the user who launches them. Deleting a file with rm usually does not move it to the trash, and the redirection operator > can overwrite an existing file’s contents. Do not run copied instructions with sudo—a tool that lets you execute commands with another user’s permissions—until you understand what they do.

To get started, modify the example script so that it only prints filenames using printf instead of moving them with mv. Checking the list before making changes is a simple habit that will also help when working with real data.

See also: What is a shell?

Read also

Learn how PHP-FPM handles PHP requests, how it differs from a web server, and how to tune processes for traffic and hosting resources.
Learn how BIND, open-source DNS server software, handles domains, how its two roles differ and when running it yourself makes sense.
Learn what Poland’s REGON register and identification number mean, how they differ from NIP and KRS, and how to check a business’s details.