Where to start First steps track · 15 stages
Linux Labs

bash — the command shell

5 min Ubuntu 24.04; Bash 5.2.21

A command interpreter for interactive input, builtins, and shell scripts.

Cheat sheet

6 commands Printable version
  • bash --version shell version
  • help builtin help
  • type pwd command type
  • help pwd command help
  • set -o shell options
  • history session history

Overview

bash is a shell: a program that interprets a command language and arranges execution. The terminal handles input and display; Bash decides how to execute the entered line. Script construction is covered separately in bash-scripting.

A command may be a builtin, function, alias, or external program. Finding an executable with a matching name does not establish that it will be selected. Use type to inspect command resolution. The cd builtin changes the current shell directory; a separate process cannot change its parent session’s directory.

Options

9

Distinguish bash invocation options from set options and shopt settings: these are separate interfaces, not one flat option list. bash script.sh runs a script in another shell. source script.sh runs it in the current one and can change its variables or directory.

Use set -o to inspect shell options and shopt to inspect additional settings. These diagnostic invocations do not change the mode. Before copying settings from an example, identify which shell should receive them.

Option What it does Example
-c command Execute command as a string and exit, without reading a script file. bash -c 'echo hi'
-x Print each command before running it (xtrace). bash -x script.sh
-e Exit on the first command with a non-zero exit status (same as set -e). bash -e script.sh
-i Start an interactive shell even when input is not a terminal. bash -i
-l --login Behave as a login shell (read the system profile and one user profile). bash -l
-n Check the script's syntax only, without executing it. bash -n script.sh
--noprofile Skip the system and per-user profile files for a login shell. bash --noprofile
--norc Skip ~/.bashrc for an interactive non-login shell. bash --norc
--version Show the bash version and exit. bash --version

Nothing found. Try a shorter query.

Recipes

4

Find what will execute

Inspect all definitions of the name. Before following an example, check whether the command has been overridden.

bash
type -a pwd
help pwd

Inspect command-line editing mode

set -o lists option states, including emacs and vi. The introductory lesson assumes Emacs editing mode.

bash
set -o

Edit the command line with keys

In emacs mode (on by default), you edit the line before Enter with these keys:

Keys What they do
Ctrl+A / Ctrl+E cursor to the start / end of the line
Alt+B / Alt+F one word back / forward
Alt+Backspace erase the word before the cursor
Ctrl+U erase everything before the cursor
Ctrl+K erase from the cursor to the end of the line
Ctrl+Y paste back the last erased text
Ctrl+R search the command history
Ctrl+L clear the screen

Text erased with Alt+Backspace, Ctrl+U and Ctrl+K isn't lost: Ctrl+Y pastes it at the cursor. That's how you move part of a command: Ctrl+U, then edit, then Ctrl+Y.

In a browser terminal Ctrl+W closes the tab, so erase words with Alt+Backspace. If Alt+B or Alt+F types a character instead of moving (it happens on macOS), use the arrow keys.

Understand startup files

An interactive non-login shell reads ~/.bashrc. A login shell reads the system profile and the first available user profile among ~/.bash_profile, ~/.bash_login, and ~/.profile. Do not infer the mode solely from how a terminal window was opened; Ubuntu also has system configuration.

bash
shopt login_shell
printf "%s\n" "$-"

Troubleshooting

3

command not found

Check spelling and command lookup. Starting another Bash does not install a missing program.

bash
type whoami
printf "%s\n" "$PATH"

A setting exists in a file but is not active

Identify the session mode and relevant startup file. Do not source an unreviewed file merely to diagnose loading behaviour.

bash
shopt login_shell
printf "%s\n" "$-"

History is empty or differs between sessions

History belongs to a shell session and its configuration. Inspect its state; printing settings does not recreate missing entries. Do not publish a whole history, which may contain secrets.

bash
set -o
printf "%s\n" "${HISTSIZE-unset}"

Danger zone

Danger. Text passed to bash -c and files read by source are executed, not merely displayed. bash -n checks syntax, not safety. Avoid tracing secret-bearing commands into shared logs. Do not treat set -euo pipefail as a correctness guarantee: behaviour depends on context and failures need deliberate handling.

Terms

3
terminal
An input and output interface for text programs. A shell interprets the command language. Also appears in: Environment variables and PATH, mc, Locale and encoding, ps and 52 more
Readline
A command-line editing library providing history navigation and key bindings in Bash.
shell
A command interpreter: it reads a command line, expands variables and patterns, and starts processes. Bash is the default interactive shell on many distributions. Also appears in: hash, nologin, How a command is built, ulimit and process limits: open files, processes, core and 58 more

See also

  • bash-scripting — variables, conditions, and scripts.

  • env-vars — environment and PATH.

  • man-help — finding the right help.

  • pwd — the shell’s current directory.

  • files-basics — basic terminal and file operations.

  • command-syntax — how the shell splits a line into a command, options and arguments.

  • redirection — output redirection and pipelines.

Loading…