Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Part 1 · Basics · Lesson 02 · 35 minutes

Edit and repeat commands with keys, history and Tab

After this lesson you can
  • Edit a command line with keys, and drop a dangerous line safely
  • Interrupt, suspend and end a program with keys, and look up a command with man
  • Repeat earlier commands with the history and finish names with Tab
Lab files of this lesson (1)

The lab folder as it stands after this lesson. Unzip it in ~/krlab.

More

The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.

Scenario

You spend your first days on a server and type the same long commands again and again. One wrong letter, and you retype the whole line. Your team lead watches and says: "A good analyst types less. Learn the keys of the shell before the end of the day." She gives you a small test in your lab: change a line without retyping it, stop a program and start it again, repeat an old command, and let the shell finish a name for you.

Recall

What does "cd -" do, and how is it different from "cd .."?

cd - returns to the directory that you were in before. cd .. goes up one level, to the parent of the directory that you are in.

What does the option -d change when you run "ls -ld /tmp"?

It lists the directory itself, as one line, and not the files inside it.

The idea

The shell is the program that reads the line that you type and runs it. It has a small editor built in, so you can change a line before you press Enter. It also remembers every line that you ran, and it can finish a name for you.

Every key below works on the command line. Ctrl+A means: hold Control and press A. Alt+F means: hold Alt and press F.

KeysWhat they do
Ctrl+A, Ctrl+EMove the cursor to the start, or to the end, of the line.
Alt+B, Alt+FMove one word back, or one word forward.
Ctrl+WDelete the word before the cursor.
Ctrl+U, Ctrl+KDelete from the cursor to the start, or to the end, of the line.
Ctrl+YPaste back what the last delete removed.
Ctrl+LClear the screen.
Ctrl+CInterrupt: stop the program that runs now.
Ctrl+ZSuspend: freeze the program and give the prompt back.
Ctrl+DEnd of input. On an empty line it closes the shell.
Ctrl+RSearch the history.
Ctrl+S, Ctrl+QFreeze the screen, and release it. Press Ctrl+Q when a terminal seems dead.

A line goes through these steps. The editor works while you type. The other steps run when you press Enter.

What the shell does with one line
Line editorHistory expansionHistory listCommand runsyou press Enter!! is replaced; the line is savedthe shell runs the linethe next prompt;Ctrl+R reads the listLine editorHistory expansionHistory listCommand runs1234
  1. Line editor → History expansion: you press Enter
  2. History expansion → History list: !! is replaced; the line is saved
  3. History list → Command runs: the shell runs the line
  4. Command runs → Line editor: the next prompt; Ctrl+R reads the list

Worked example

1. Ask the machine

You do not need to remember every option. The machine holds its own manual:

  • COMMAND --help prints a short summary of most tools.
  • man COMMAND (manual) opens the full page in a viewer. Space shows the next screen, /word and Enter search, n finds the next match, and q leaves.
  • man -k WORD (also apropos WORD) searches the one-line descriptions of all pages. Use it when you know the job but not the name.
  • whatis NAME prints the one-line description of a command.
  • type -a NAME shows what the shell runs under that name: a file, an alias or a built-in command.

head -n 3 keeps the first three lines of what comes in, so this prints only the top of the help text.

ls --help | head -n 3
whatis ls mkdir
type -a ls
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
ls (1)               - list directory contents
mkdir (1)            - make directories
ls is /usr/bin/ls
ls is /bin/ls

/bin is a link (another name for a folder; lesson 8) to /usr/bin, so both lines name the same file. In your own terminal, type -a ls can also print ls is aliased to first (lesson 3 explains aliases). man -k "make directories" shows how a search works: it prints every page whose description holds those words, and the first lines differ from one machine to the next. On Ubuntu 26.04 and other newer releases, the help text and the whatis lines use other words, because that release uses a rewrite of the basic tools (uutils coreutils). The commands work the same. If man says that the command or the page is missing, install the package: sudo apt install man-db.

2. Edit a line with keys

echo prints text. A ; ends one command, so that a second one can follow on the same line. Type echo world, press Ctrl+A, type echo hello; and press Enter.

hello
world

Type echo one two three, press Ctrl+W, type four and press Enter. The word three is gone.

one two four

Type rm -r important and stop. Press Ctrl+U to wipe the line, so that nothing runs, and type echo changed my mind. This is the safe way to drop a dangerous line.

3. Interrupt, suspend and end input

sleep 30 waits 30 seconds and prints nothing. Run it, then press Ctrl+C. The shell prints ^C and gives the prompt back.

Run sleep 31 and press Ctrl+Z. The program is frozen, not dead. jobs lists the frozen programs, and fg (foreground) wakes the last one. Press Ctrl+C to end it.

sleep 31
jobs
fg
^Z
[1]+  Stopped                    sleep 31
[1]+  Stopped                    sleep 31
sleep 31
^C

sort puts lines in order. Without a file it reads what you type, line by line, until you press Ctrl+D. That key means "no more input".

sort

Type pear, apple and fig, each followed by Enter, and then press Ctrl+D.

apple
fig
pear

4. Reuse earlier commands

history lists the lines that you ran, with numbers (history 5 lists the last five). !! runs the last line again. !40 runs the line with the number 40. Run three echo lines, repeat the last one, and list the history.

echo one
echo two
echo three
!!
history 5
one
two
three
echo three
three
   39  echo one
   40  echo two
   41  echo three
   42  echo three
   43  history 5
One line of the history list
41the number: !41 runs itecho threethe line that you typed41the number: !41 runs itecho threethe line that you typed

The shell prints the old line before it runs it. Your numbers are different. Read the number of echo two in your list, and run it with ! and that number. Check the line first: !40 runs whatever line 40 holds, also a rm.

!40
echo two
two

On a real server the idiom sudo !! runs the last line again with root rights. It is not run here, because sudo needs a password.

Ctrl+R searches the history while you type. Press it, then type echo t. The shell shows the newest line that holds the text. Press Ctrl+R again for an older one, Enter to run the line, Esc to take the line to the prompt and edit it, or Ctrl+G to give up.

(reverse-i-search)`echo t': echo three

5. Let Tab finish a name

Tab asks the shell to complete the name that you started. Make three folders to try it on.

mkdir -p tab/alpha tab/alpine tab/beta

Type ls tab/be and press Tab. The line becomes ls tab/beta/. Press Enter: the folder is empty, so ls prints nothing.

When the start is not unique, the shell adds only the part that all choices share. Type ls tab/al and press Tab: the line becomes ls tab/alp. If the line does not change, press Tab a second time. The shell then lists the choices.

alpha/  alpine/

Type h and press Tab, and the line is ls tab/alpha/. Tab also completes the names of commands: type his and press Tab to get history.

Your turn

Do the test of your team lead. There are no steps here.

Make the folder ~/krlab/linux-basics/keys and go into it. In it, make the folders audit-2025, audit-2026 and backup-2026. Do the five jobs inside keys. Then:

  1. Type ls au and press Tab twice, so that the shell lists its choices.
  2. Type rm -r backup-2026 and do not press Enter. Clear the line with one key combination, so that nothing runs.
  3. Run echo audit finished. Then press Ctrl+R, type audit and press Enter, to run the line again from the history.
  4. Run sort, type three words, and end the input with a key.
  5. Open man ls, search for human, and leave the page.

Check yourself

You press Ctrl+C in a running program and nothing changes. What can you do?

Press Ctrl+Z to suspend it and get the prompt back, then stop it from there (Part 2 shows kill). Also check that the terminal is not frozen by Ctrl+S: press Ctrl+Q.

Why must you read the line before you run "!40"?

!40 runs whatever line number 40 holds now, and that can be a rm. The shell prints the old line before it runs it, but it does not ask. Read the number in your own history, or use Ctrl+R, which shows the line first.

You press Tab twice and the shell lists nothing. What does that mean?

No name starts with the text that you typed, so there is nothing to complete. Check the spelling and the folder. When the shell adds some letters and stops, several names share them: type one more letter, or press Tab again to see the choices.

What is the difference between Ctrl+C and Ctrl+Z?

Ctrl+C stops the program for good. Ctrl+Z only freezes it: jobs lists it, and fg wakes it.

Field notes

  • History keeps what you type, and ~/.bash_history is a plain file. A password in a command line stays there. With HISTCONTROL=ignorespace, bash does not save a line that starts with a space. Better: keep secrets out of command lines.
  • For an investigator, ~/.bash_history is a clue. The shell writes it when it exits, and the user can empty it, so a missing line proves nothing.
  • On a real server, sudo !! runs the last line again with root rights. Read the line first: it works on whatever ran last, not on what you meant.