VMware ESXi and vSphere Cluster Management

Bash: The Bourne-Again Shell in Linux

Learn what Bash is, how it interprets Linux commands, and how to use globbing, pipelines, variables, history, tab completion, and scripts.

Bash is the Bourne Again SHell: a shell and command-language interpreter commonly used on Linux. It provides a text-based interface between you and the operating system. You type a command, Bash interprets it, runs the requested operation or program, and displays output or an error.

This lesson explains Bash as both an interactive command environment and a language for simple automation. You can return to this Bash reference while practicing.

What Bash Is

A shell is a program that reads commands and coordinates their execution. A command interpreter parses command text, performs shell operations, and starts other programs when necessary.

A typical interaction follows this cycle:

  1. Bash displays a prompt to show that it is ready.
  2. You type a command, such as pwd, and press Enter.
  3. Bash interprets the command, including any expansions or special operators.
  4. Bash runs a built-in operation or an external program.
  5. The command writes output, an error, or both, and Bash displays another prompt.
$ pwd
/home/alex
$

In this example, $ is part of the prompt, pwd is the command, and /home/alex is its output. The exact prompt text varies between systems.

The Name and Origins of Bash

Bash stands for Bourne Again SHell. It is a shell developed under the GNU Project and created by Brian Fox. Bash was designed as a compatible and enhanced successor to the original Unix Bourne shell, commonly invoked as sh.

Compatibility means that many commands and scripts written for the older Bourne shell can work in Bash. Bash also adds interactive conveniences and language features, including command history, completion, pipelines, variables, and scripting constructs.

Bash, Linux, and the Terminal

Bash is commonly installed and commonly selected as the default login or interactive shell on Linux systems. However, the distribution, system administrator, or individual user can select another shell, such as sh, zsh, or fish.

A terminal emulator is the application window or text-terminal program that provides access to a shell. It is not necessarily the shell itself. The terminal handles the display and keyboard connection; Bash runs inside it and interprets your commands.

ComponentRoleExamples
Terminal emulatorProvides a window or terminal session for interacting with a shellDesktop terminal application, virtual console
ShellReads and interprets command textBash, sh, zsh, fish
External commandA separate executable started by the shellls, sort, date
Shell built-inAn operation implemented inside the shellcd, export, history

To inspect the shell named for your login environment, use:

echo "$SHELL"

This value indicates the account's configured login shell; it does not always prove which shell is currently interpreting every command. To identify Bash's version when Bash is running, use:

bash --version

Running bash starts a Bash session from another shell. Type exit to leave that nested session.

Interactive Commands

A common command structure is:

command option argument
  • The command names an operation or program.
  • An option modifies behavior and often begins with a hyphen.
  • An argument supplies input, such as a filename, directory, or value.
ls -l Documents

Here, ls is the command, -l is an option requesting a long listing, and Documents is an argument naming a directory. Press Enter to submit a command. Bash may execute a built-in such as cd, or search for and start an external program such as ls.

Wildcard Expansion and Globbing

Filename globbing, also called wildcard expansion, is Bash's process of matching a pattern against filenames before the command runs. A glob is a filename-matching pattern; a wildcard is one of its pattern characters.

PatternMatchesExample
*Any sequence of characters in a filename component*.txt matches names ending in .txt
?Exactly one characterfile?.txt matches file1.txt
[abc]One character from the listed setfile[abc].txt
[0-9]One character in the specified rangeimage[0-9].png
ls *.txt

If the directory contains notes.txt and todo.txt, Bash effectively supplies those matching names to ls. The ls program does not normally perform this expansion itself.

Globs are not regular expressions. A glob's * matches a sequence of filename characters, while regular-expression syntax uses different rules and is usually interpreted by text-processing tools such as grep. Quoting a pattern, as in "*.txt", prevents Bash from expanding it.

Pipelines and Command Input and Output

Commands commonly communicate through standard input and standard output. Standard input is the stream a command reads, often from the keyboard. Standard output is the stream a command writes, often to the terminal. Error messages commonly use a separate standard-error stream.

The pipe operator, |, connects the first command's standard output to the second command's standard input:

ls -1 | sort

ls -1 produces one filename per line, and sort reads those lines and sorts them. A pipeline can chain several commands:

history | tail

This displays the end of the command-history list. If a pipeline gives an unexpected result, remember that diagnostics may be written to standard error rather than standard output, and the receiving command may expect a different input format.

Command Substitution

Command substitution inserts a command's output into another command or assignment. Use the modern $(...) notation:

echo "Today is $(date +%F)"

Bash runs date +%F, captures its output, and places that text inside the argument given to echo. The older backtick form, `command`, exists for compatibility but is less readable, especially when substitutions are nested.

Shell Variables and the Environment

A variable stores a named shell value, commonly text. Assign a value with no spaces around the equals sign, then retrieve it with a dollar sign:

project="demo"
echo "$project"

The quotes around "$project" preserve the value as one argument if it contains spaces. This is a useful general habit when expanding variables.

An environment variable is a value exported by the shell so that programs started by the shell can inherit it. HOME commonly identifies a user's home directory. PATH contains directories Bash searches for executable commands.

echo "$HOME"
echo "$PATH"
command -v ls

To make a shell variable available to child processes, export it:

project="demo"
export project

History and Command Recall

Bash records previously entered commands in a history. Press the Up Arrow to recall older commands and the Down Arrow to move toward newer commands. You can edit a recalled command before pressing Enter.

history | tail

The history command prints recent history entries. Bash also supports optional history expansion, such as !! for the previous command, depending on the session settings. Treat history expansion carefully: always review a recalled command before rerunning it, especially when it can overwrite or remove files.

Tab Completion

Press Tab while typing to ask Bash to complete a command name, path, or filename. If the text has one unique match, Bash can complete it. If several matches are possible, Bash may complete the shared portion and leave the remaining choice for you. Pressing Tab twice displays available completions in many configurations.

cd Doc<Tab>

If Documents is the only matching directory, Bash can complete the command to cd Documents. Add more characters when several names begin with the same prefix.

Bash Feature Overview

FeatureWhat it doesTypical syntax or keyBeginner use case
Wildcard expansionMatches filename patterns before execution*.txtSelect text files without typing every name
PipelinesSends one command's output to another's inputcommand1 | command2Filter, sort, or transform command output
Command substitutionEmbeds command output in another command$(command)Use a date, directory, or status value dynamically
VariablesStores and expands named valuesname=value, $nameReuse paths or settings
Command historyRecalls previously entered commandsUp Arrow, historyReuse and edit a long command
Tab completionCompletes available command and path textTabAvoid typing long filenames

Interactive Bash Versus Bash Scripts

The same language used interactively can be placed in a Bash script for repeatable automation. A script normally begins with a shebang, an interpreter directive on the first line. It then contains commands and may be given executable permission.

#!/usr/bin/env bash
echo "Hello from Bash"
printf '%s\n' '#!/usr/bin/env bash' 'echo "Hello from Bash"' > hello.sh
chmod +x hello.sh
./hello.sh

The shebang asks the system to use Bash to interpret the file. Bash is effective for command orchestration, file operations, and small automation tasks, but it is not a general-purpose replacement for every programming language. Larger applications may be clearer and safer in a language designed for that domain.

Quoting and Basic Safety

Shell syntax gives spaces and characters such as *, $, |, and ; special meaning. Quote filenames containing spaces or special characters:

ls "Project Notes.txt"
cd 'Directory With Spaces'

Before a file-altering command, check the current directory with pwd and inspect the names that a wildcard will expand to. Commands can change or remove files immediately, subject to permissions and command options. Be especially cautious with recursive or force options and with commands that use variables or wildcards.

Troubleshooting Common Bash Problems

Tab does not complete a filename

  • The prefix may match multiple paths. Type more characters, or press Tab again to request possible matches.
  • A path containing spaces may need quotes or backslash escaping, such as cd "My Documents".
  • Completion support may not be enabled in the current environment.

A wildcard appears unchanged

  • No filename may match the pattern. Check the location with pwd and inspect it with ls.
  • The wildcard may be quoted, which intentionally prevents expansion. Remove the quotes only when expansion is wanted.
  • Bash options can change how unmatched patterns are handled.

A command is reported as not found

  • Check the spelling and use completion where available.
  • The program may not be installed.
  • Its directory may be absent from PATH. Inspect the search path with echo "$PATH" and try command -v command_name.

A variable prints as empty

  • Check that the variable name is spelled consistently.
  • Use name=value, not name = value; spaces would be parsed as separate command words.
  • If a child process must receive the value, use export name.

A pipeline gives an unexpected result

  • The first command may write diagnostics to standard error instead of standard output.
  • The second command may expect a different input format.
  • Test each command separately, then combine simple known-good commands. Redirection of standard error is an additional topic to learn after standard input and output are clear.

Exam- and Practice-Relevant Notes

  • Bash is both a shell and a command-language interpreter.
  • A terminal emulator provides access to Bash; it is not automatically Bash itself.
  • Shell expansion occurs before a command receives its arguments. Globs are not regular expressions.
  • The pipe operator connects standard output to standard input.
  • Variable assignment has no spaces around =; expansion uses $name.
  • PATH controls where Bash searches for executable commands, while HOME commonly identifies the user's home directory.
  • History and completion improve usability, but recalled commands and wildcard expansions should be reviewed before potentially destructive execution.