bash lets you display or modify previous commands. This is similar to the C shell's history mechanism. Commands in the history list can be modified using:
Line-edit mode
The fc command
In addition, the command substitutions described in Chapter 8, "csh and tcsh", also work in bash.
Line-edit mode lets you emulate many features of the vi or Emacs editors. The history list is treated like a file. When the editor is invoked, you type editing keystrokes to move to the command line you want to execute. Arrow keys work on most terminals in both Emacs mode and vi command mode. You also can change the line before executing it. See Table 7-23 for some examples of common line-edit commands. When you're ready to issue the command, press Return.
The default line-edit mode is Emacs. To enable vi mode, enter:
$ set -o vi
Note that vi starts in input mode; to type a vi command, press Esc first.
The mode you use for editing bash commands is an entirely separate choice from the editor that is invoked for you automatically within many commands (for instance, the editor mail readers invoke when you ask them to create a new mail message). To change the default editor, set the VISUAL or EDITOR variable to the filename or full pathname of your favorite editor:
$ export EDITOR=emacs
vi | Emacs | Result |
---|---|---|
k | Ctrl-P | Get previous command. |
j | Ctrl-N | Get next command. |
/string | Ctrl-R string | Get previous command containing string. |
h | Ctrl-B | Move back one character. |
l | Ctrl-F | Move forward one character. |
b | Esc B | Move back one word. |
w | Esc F | Move forward one word. |
X | Del | Delete previous character. |
x | Ctrl-D | Delete one character. |
dw | Esc D | Delete word forward. |
db | Esc H | Delete word back. |
xp | Ctrl-T | Transpose two characters. |
Use fc -l to list history commands and fc -e to edit them. See the entry under built-in commands for more information.
$ history List the last 16 commands $ fc -l 20 30 List commands 20 through 30 $ fc -l -5 List the last five commands $ fc -l cat List the last command beginning with cat $ fc -ln 5 > doit Save command 5 to file doit $ fc -e vi 5 20 Edit commands 5 through 20 using vi $ fc -e emacs Edit previous command using Emacs $ !! Reexecute previous command $ !cat Reexecute last cat command $ !cat foo-file Reexecute last command, adding foo-file to the end of the argument list
Syntax | Meaning |
---|---|
! | Begin a history substitution. |
!! | Previous command. |
!N | Command number N in history list. |
!-N | Nth command back from current command. |
!string | Most recent command that starts with string. |
!?string? | Most recent command that contains string. |
!?string?% | Most recent command argument that contains string. |
!$ | Last argument of previous command. |
!# | The current command up to this point. |
!!string | Previous command, then append string. |
!N string | Command N, then append string. |
!{s1}s2 | Most recent command starting with string s1, then append string s2. |
^old^new^ | Quick substitution; change string old to new in previous command; execute modified command. |
Using the following variables, you can display information about the current state of the shell or the system in your bash prompt. Set the PS1 variable to a string including the desired variables. For instance, the following command sets PS1 to a string that includes the \w variable in order to display the current working directory and the \! variable in order to display the number of the current command. The next line is the prompt displayed by the change.
$ PS1='\w: Command \!$ ' ~/book/linux: Command 504$
Some of the prompt variables are relatively new, such as \j and \l, so they may not be supported in your version of bash.
Variable | Meaning |
---|---|
\a |
Alarm (bell) |
\d |
Date in the format "Mon May 8" |
\e |
Escape character (terminal escape, not backslash) |
\h |
Hostname |
\j |
Number of background jobs (active or stopped) |
\l |
Current terminal name |
\n |
Newline inserted in the prompt |
\r |
Carriage return inserted in the prompt |
\s |
Current shell |
\t |
Time in 24-hour format, where 3:30 p.m. appears as 15:30:00 |
\u |
User's account name |
\v |
Version and release of bash |
\w |
Current working directory |
\H |
Like \h |
\T |
Time in 12-hour format, where 3:30 p.m. appears as 03:30:00 |
\V |
Version, release, and patch level of bash |
\W |
Last element (following last slash) of current working directory |
\\ |
Single backslash inserted in the prompt |
\! |
Number of current command in the command history |
\# |
Number of current command, where numbers started at 1 when the shell started |
\@ |
Time in 12-hour format, where 3:30 P.M. appears as 03:30 p.m. |
\$ |
Indicates whether you are root: displays # for root, $ for other users |
\[ |
Starts a sequence of nonprinting characters, to be ended by \] |
\] |
Ends the sequence of nonprinting characters started by \[ |
\nnn |
The character in the ASCII set corresponding to the octal number nnn inserted into the prompt |
Copyright © 2001 O'Reilly & Associates. All rights reserved.