- 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:- You type a command and press Enter.
- Bash parses and interprets the command line.
- Each command on the line is either a shell builtin or an external program.
- Bash invokes the builtin directly or locates the external program and executes it, then prints output or errors.
Interactive demo: basic commands, chaining, and shortcuts
Example session showing common commands, command chaining, and simple use of Tab completion, history, and aliases:;— 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.
user then pressing Tab twice):
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 numberNfrom the history output.
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.
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.
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.
- Embed the output of
date:
- Use a pipeline to count lines produced by
ls -l(note:ls -lmay include a “total” line):
- Capture command output into a variable:
|) streams output between processes at runtime; command substitution $(...) captures output and places it into the command line or a variable.
Summary
Links and references
- Bash Reference Manual — https://www.gnu.org/software/bash/manual/
- Bash guide (GNU/Linux) — https://www.gnu.org/software/bash/
- Bash completion project — https://github.com/scop/bash-completion