The shell keeps a list of every line that you send, also the lines that fail. This list is the history, and a new command shows it.
history
Lists every line that you sent in this terminal, oldest first, each with a number. The first line has the number 1.
The shell also has keys and short forms to bring a line back:
| You do | What happens |
|---|---|
| Push the up arrow | Your last line comes back after the prompt. Push it again to go one more line back. |
Run !! | The shell runs the last line again. |
Run !3 | The shell runs line number 3 again. |
To run a line by its number, run history first and find the number on the left of that line. Then type ! and the number together, with no space, and push Enter:
learner@tidepool:~$ history
1 echo hello
2 expr 2 + 2
3 hostname
4 history
learner@tidepool:~$ !3
hostname ← bash repeats our command
tidepool ← bash shows the output of it!3 ran line 3, hostname, again. ! 3 with a space does not work, and neither does ! and the number on two lines.
Objectives
- Run
whoami,hostnameandecho hello, in this order. - Bring back
hostnameusing your arrow keys. - Can you run your most recent command again using the trick in the lesson?
- Run
whoamiagain by its number.