LinuxII · Shell and Command-Line OperationsProductivity
Shell history, aliases, and functions
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
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.
$ history 5 42 uname -r
43 systemctl status ssh
44 journalctl -u ssh --since '1 hour ago'
45 ls -la /etc/ssh
46 history 5Illustrative output
Useful keybindings (Emacs-style by default; switch to vi with
set -o vi):
| Key | Action |
|---|---|
Ctrl-R | Reverse incremental search through history. Type a substring; Ctrl-R cycles through matches. Press Enter to run, Tab to edit. |
Ctrl-P / Up arrow | Previous command |
Ctrl-N / Down arrow | Next command |
!! | Re-run the last command |
!42 | Re-run command 42 from history |
!ssh | Re-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:
$ alias ll=ls-lh; alias grep=grep-color-auto; alias rm=rm-i; aliasalias ll=ls-lh
alias grep=grep-color-auto
alias rm=rm-i
aliasIllustrative 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:
$ lso() { ls -lh "$@" | less -F; }; type lsolso 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
| What | Where |
|---|---|
| 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
Q1. Which keybinding performs reverse incremental search through bash history?
Q2. A shell alias defined in ~/.bashrc is available in cron scripts.
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.