Skip to main content
RunBook Academy

LinuxII · Shell and Command-Line OperationsProductivity

Shell history, aliases, and functions

Foundation⏱ ~8 minbashhistoryaliastype

What you'll learn

  • Configure and search shell history effectively
  • Define aliases for common shortcuts
  • Define shell functions for multi-line productivity
  • Identify when to put each into .bashrc vs .profile

Prerequisites

Verified against Ubuntu 24.04 LTS · Debian 12 (Bookworm) · RHEL 9.x · Rocky Linux 9.x · AlmaLinux 9.x · Linux kernel 6.1 LTS / 6.6 LTS · systemd 255+ · OpenSSH 8.7p1 (RHEL 9) / 9.6p1 (Ubuntu 24.04) · nftables 1.0.x · chrony 4.x · Pacemaker 2.1.x · Corosync 3.1.x · 2026-08-09

Not yet marked complete on this device.

Small shell-quality investments — better history, well-chosen aliases, named functions — repay themselves every day.

Shell history

Bash records every interactive command in memory and writes the session to ~/.bash_history (or wherever HISTFILE points) when the shell exits.

Read-only / Safehistory
$ history 5
   42  uname -r
43  systemctl status ssh
44  journalctl -u ssh --since '1 hour ago'
45  ls -la /etc/ssh
46  history 5

Illustrative output

Useful keybindings (Emacs-style by default; switch to vi with set -o vi):

KeyAction
Ctrl-RReverse incremental search through history. Type a substring; Ctrl-R cycles through matches. Press Enter to run, Tab to edit.
Ctrl-P / Up arrowPrevious command
Ctrl-N / Down arrowNext command
!!Re-run the last command
!42Re-run command 42 from history
!sshRe-run the most recent command that started with ssh
!$The last argument of the previous command
!^The first argument of the previous command
Alt-.Cycle through the last argument of previous commands

Aliases

An alias is a name that expands to a command when typed:

Read-only / Safealias list
$ alias ll=ls-lh; alias grep=grep-color-auto; alias rm=rm-i; alias
alias ll=ls-lh
alias grep=grep-color-auto
alias rm=rm-i
alias

Illustrative output

Shell functions

A shell function is a named block of shell code that you can call later. Functions are available in scripts (unlike aliases) and can take arguments:

Read-only / Safeshell function
$ lso() { ls -lh "$@" | less -F; }; type lso
lso is a function
lso ()
{
ls -lh "$@" | less -F
}

Illustrative output

A practical pattern for sysadmins — a function that wraps a complex diagnostic:

sysd-journal() {
    journalctl -u "$1" --since "${2:-1 hour ago}" --no-pager
}

Then: sysd-journal ssh '1 hour ago' or just sysd-journal ssh.

Where to put each

WhatWhere
Interactive aliases and functions~/.bashrc (interactive non-login shells)
Environment (PATH, EDITOR, LANG)~/.profile or ~/.bash_profile
System-wide aliases/etc/profile.d/ (Debian-family) or /etc/bashrc (RHEL-family)

WhyThisMatters

Knowledge check

Knowledge check · 3 questions

  1. Q1. Which keybinding performs reverse incremental search through bash history?

  2. Q2. A shell alias defined in ~/.bashrc is available in cron scripts.

  3. Q3. Which of the following are appropriate uses of shell functions in production sysadmin work? Select all that apply.

Passing score: 75%. Answers are checked in this browser.