Skip to main content
In this lesson you’ll learn what actually happens when you run commands in the Bash shell. After reading, command execution should feel predictable and easy to reason about — a useful foundation for hands-on practice. Environment: Red Hat Enterprise Linux 10 (virtual machine). Goals:
  • Understand how Bash receives and runs commands.
  • Use command chaining, completion, history, aliases, variables, and command substitution effectively.

How Bash executes a command

At a high level, running a command in Bash follows this flow:
  1. You type a command and press Enter.
  2. Bash parses and interprets the command line.
  3. Each command on the line is either a shell builtin or an external program.
  4. Bash invokes the builtin directly or locates the external program and executes it, then prints output or errors.
Bash is the interface between you and the operating system. Knowing whether a command is a builtin or an external program (and how Bash finds it) makes behavior more predictable.

Interactive demo: basic commands, chaining, and shortcuts

Example session showing common commands, command chaining, and simple use of Tab completion, history, and aliases:
Notes:
  • ; — executes the next command regardless of the previous command’s exit status.
  • && — executes the next command only if the previous command returned a successful exit code (0).

Command chaining operators

Use the following operators to control multi-command behavior.

Auto-completion (Tab)

Bash completion speeds up typing and reduces errors.
  • Press Tab once: Bash will complete the word if the completion is unambiguous.
  • Press Tab twice: If multiple completions exist, Bash lists the possibilities.
Example (typing user then pressing Tab twice):
When there is a single unambiguous completion (for example useradd), a single Tab completes it. If multiple matches exist, pressing Tab twice prints the options. For more advanced completions (git, kubectl, package managers), enable and configure completion scripts or packages for your distribution.

History and re-running commands

Bash keeps a history of commands which you can inspect and reuse.
  • history — lists previously executed commands with numbers.
  • Up/Down arrow keys — navigate interactively through recent commands.
  • !N — re-run the command number N from the history output.
Example:
Tip: Use history | grep <term> to search your history quickly.

Aliases

Aliases create short names for frequently used commands.
  • Define an alias: alias ll='ls -l'
  • Remove an alias: unalias ll
  • Aliases set at the prompt last only for that session — to persist them, add them to ~/.bashrc.
Example:
Aliases created at the shell prompt last only for that session. To make an alias persistent, append it to your ~/.bashrc file (for example: echo "alias ll='ls -l'" >> ~/.bashrc).

Variables

Shell variables hold reusable values. Key rules:
  • Assign without spaces around =: MYVAR="hello world"
  • Refer to a variable using $MYVAR.
  • Variables defined in the shell are not exported to child processes unless you use export.
Example:
To make a variable available to child processes, use export, for example: export MYVAR="hello world". To persist variables across sessions, add the export line to ~/.bashrc.

Command substitution and pipelines

Combine commands by passing output between them or capturing output for use in other commands.
  • Command substitution: $(...) — preferred form for readability and nesting (over backticks).
  • Pipe: | — sends the standard output (stdout) of one command to the standard input (stdin) of the next.
Examples:
  1. Embed the output of date:
  1. Use a pipeline to count lines produced by ls -l (note: ls -l may include a “total” line):
  1. Capture command output into a variable:
Note: Piping (|) streams output between processes at runtime; command substitution $(...) captures output and places it into the command line or a variable.

Summary


This concludes the lesson.

Watch Video

Practice Lab