> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demo Executing Commands with the Bash Shell

> Explains how Bash executes commands and how to use chaining, completion, history, aliases, variables, command substitution, and pipelines for effective interactive shell use.

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:

```bash theme={null}
student@control:~$ date
Sun Apr  5 11:02:34 AM EEST 2026
student@control:~$ pwd
/home/student
student@control:~$ whoami
student
student@control:~$ ls ; pwd
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
/home/student
student@control:~$ ls && echo test
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
test
student@control:~$ touch emptyfile
student@control:~$ ls
Desktop  Documents  Downloads  emptyfile  Music  Pictures  Public  Templates  Videos
student@control:~$
```

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.

| Operator | Behavior | Example |
| - | - | - |
| `;` | Run next command regardless of exit status | `cmd1 ; cmd2` |
| `&&` | Run next command only if previous succeeded (`exit 0`) | `cmd1 && cmd2` |

***

## 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):

```bash theme={null}
student@control:~$ user
useradd  userdbctl  userdel  usermod  users
```

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:

```bash theme={null}
student@control:~$ history
1  clear
2  ls
3  clear
4  hostnamectl hostname control
5  exit
6  date
7  pwd
8  whoami
9  ls ; pwd
10 ls && echo test
11 touch emptyfile
12 ls
13 clear
14 history
student@control:~$ !12
Desktop  Documents  Downloads  emptyfile  Music  Pictures  Public  Templates  Videos
student@control:~$
```

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:

```bash theme={null}
student@control:~$ alias ll='ls -l'
student@control:~$ ll
total 4
-rw-r--r-- 1 student student 0 Apr  5 11:02 emptyfile
student@control:~$ unalias ll
student@control:~$ alias
alias alert='echo -e "\a"'
# (other default aliases may appear)
```

<Callout icon="lightbulb" color="#1CB2FE">
  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`).
</Callout>

***

## 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:

```bash theme={null}
student@control:~$ MYVAR="hello world"
student@control:~$ echo "$MYVAR"
hello world
student@control:~$
```

<Callout icon="lightbulb" color="#1CB2FE">
  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`.
</Callout>

***

## 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`:

```bash theme={null}
student@control:~$ echo "Today is $(date)"
Today is Sun Apr  5 11:15:36 AM EEST 2026
student@control:~$
```

2. Use a pipeline to count lines produced by `ls -l` (note: `ls -l` may include a "total" line):

```bash theme={null}
student@control:~$ ls -l | wc -l
9
student@control:~$
```

3. Capture command output into a variable:

```bash theme={null}
student@control:~$ MYDATE="$(date)"
student@control:~$ echo "$MYDATE"
Sun Apr  5 11:15:36 AM EEST 2026
student@control:~$
```

Note: Piping (`|`) streams output between processes at runtime; command substitution `$(...)` captures output and places it into the command line or a variable.

***

## Summary

| Feature | Purpose |
| -: | - |
| Command chaining (`;`, `&&`) | Control execution order and conditional execution |
| Tab completion | Speed up typing; list ambiguous completions with double-Tab |
| History & `!N` | Re-run previous commands quickly |
| Aliases | Shorten frequently used commands; persist in `~/.bashrc` |
| Variables & `export` | Store values and optionally export to child processes |
| Command substitution & pipelines | Combine output from commands to build more powerful expressions |

***

## Links and references

* Bash Reference Manual — [https://www.gnu.org/software/bash/manual/](https://www.gnu.org/software/bash/manual/)
* Bash guide (GNU/Linux) — [https://www.gnu.org/software/bash/](https://www.gnu.org/software/bash/)
* Bash completion project — [https://github.com/scop/bash-completion](https://github.com/scop/bash-completion)

This concludes the lesson.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/red-hat-certified-system-administrator-rhcsa/module/9884b7b6-7d0b-4d00-8403-fc6d3adc7bf0/lesson/260162c7-87ab-47b8-a32f-23283a41f67c" />

  <Card title="Practice Lab" icon="flask-conical" cta="Learn more" href="https://learn.kodekloud.com/user/courses/red-hat-certified-system-administrator-rhcsa/module/9884b7b6-7d0b-4d00-8403-fc6d3adc7bf0/lesson/967d7e2a-edfd-4446-a7cc-b36b3075fce9" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.