Linux shell programming study notes 38: history command

Table of contents

  1. 0 Preface
    1. 1 The function, format and exit status of the history command
    2. 1.1 Functions of the history command
    3. 1.2 Format of history command
    4. 1.3 Exit status
    5. 2 Command application examples
    6. 2.1 history: Display command history list
    7. 2.2 history -a: Append the command line history of the current session to the history file ~/.bash_history
    8. 2.3 history -c: delete all entries to clear the history list
    9. 2.4 history -d offset: delete the history list content from the specified location
    10. 2.5 history positive integer: display the specified number of historical command records
    11. 2.6 history -r: Read history files and append the contents to the history list
    12. 2.7 history -s command: append the specified command to the history list
    13. 2.8 history -w [file]: Write the current history into the history file and append it to the history list
      1. 2.8.1 Write the current history to the default history file
      2. 2.8.2 Write the current history to the specified file
    14. 2.9 !: Repeat the command
      1. 2.9.1 !! or !-1: Repeat the previous command
      2. 2.9.2 !Command history list column number: Repeat the command of the specified line in the history list
      3. 2.9.3 !String: Repeat the command starting with the specified string in the history list
    15. 2.10 Other usage tips
      1. 2.10.1 Re-execute or view commands in command history
      2. 2.10.2 Using (reverse-i-search) mode
      3. 2.10.3 Using parameters from the command history
  2. 3. Environment variables related to the history command

0 Preface

Friends who use DOS know that you can use the up and down cursor keys in the command line prompt to browse recently executed commands. This is based on the DosKey command provided by DOS.

In Unix and Linux shells, we can also use the up and down cursor keys to browse the history of recently executed commands. This is because of the history command.

Bash can save commands that have been executed in the past. When a user logs in to the shell, the ~/.bash_history file in the user's home directory will be read and the historical command list will be saved in memory. When the user exits the current shell, the historical command list in memory will be overwritten to the ~/.bash_history file.

 1 The function, format and exit status of the history command

We can use the help history command to view the help information of the history command.

purpleEndurer @ bash ~ $help history
history: history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...]
    Display or manipulate the history list.
    
    Display the history list with line numbers, prefixing each modified
    entry with a `*'.  An argument of N lists only the last N entries.
    
    Options:
      -c        clear the history list by deleting all of the entries
      -d offset delete the history entry at offset OFFSET.
    
      -a        append history lines from this session to the history file
      -n        read all history lines not already read from the history file
      -r        read the history file and append the contents to the history
        list
      -w        write the current history to the history file 
        and append them to the history list
    
      -p        perform history expansion on each ARG and display the result
        without storing it in the history list
      -s        append the ARGs to the history list as a single entry
    
    If FILENAME is given, it is used as the history file.  Otherwise,
    if $HISTFILE has a value, that is used, else ~/.bash_history.
    
    If the $HISTTIMEFORMAT variable is set and not null, its value is used
    as a format string for strftime(3) to print the time stamp associated
    with each displayed history entry.  No time stamps are printed otherwise.
    
    Exit Status:
    Returns success unless an invalid option is given or an error occurs.
purpleEndurer @ bash ~ $

1.1 Functions of the history command

The history command is an internal command that can display or manipulate the command history list.

purpleEndurer @ bash ~ $type history
history is a shell builtin
purpleEndurer @ bash ~ $

1.2 Format of history command

history [-c] [-d offset] [number of history records]

or

history -anrw [file name]

or

history -ps parameters [parameters...]

Options Function Remark
-a Append the command line history of the current session to the history file ~/.bash_history append
-c Clear the history list by deleting all entries. clear
-d offset  Delete the history list contents from the specified location. delete
Number of historical records Display the specified number of historical command records, which should be a positive integer number
-n Read all unread lines from the history file not
-p parameter Expand the history for each specified parameter and display the results without storing them in the history list perform
-r Read history file and append contents to history list read
-s command Appends the specified command to the history list as a single record. If a filename is specified, it will be used as a history file. Otherwise use the $HISTFILE variable if it has a value, otherwise use the ~/.bash_history file. If the $HISTTIMEFORMAT variable is set and not empty, its value is used in the strftime(3) format string to print the timestamp associated with each displayed history entry, otherwise no timestamp is printed. single
-w [file] Write the current history to the history file and append it to the history list write

1.3 Exit status

Returns successful unless an invalid option is used or an error occurs.


2 Command application examples

2.1 history: Display command history list

prupleEndurer @ bash ~ $ history
    1  PS1="prupleEndurer @ bash \w $ "
    2  history
prupleEndurer @ bash ~ $ 

 

 2.2 history -a: Append the command line history of the current session to the history file ~/.bash_history

prupleEndurer @ bash ~ $ cat ~/.bash_history
PS1="prupleEndurer @ bash \w $ "
history -2
echo $?
history
history -r
history
history
history -a
prupleEndurer @ bash ~ $ history -a
prupleEndurer @ bash ~ $ cat ~/.bash_history
PS1="prupleEndurer @ bash \w $ "
history -2
echo $?
history
history -r
history
history
history -a
history -n
ls ~/.bash_history
cat ~/.bash_history
cat ~/.bash_history -a
cls
clear
cat ~/.bash_history -a
cls
clear
cat ~/.bash_history
history -a
prupleEndurer @ bash ~ $ 

We first execute the cat ~/.bash_history command to view the contents of the file ~/.bash_history

Then execute the history -a command to append the command line history of the current session to the history file ~/.bash_history

Then execute the cat ~/.bash_history command again to view the contents of the file ~/.bash_history

You can see that the following command lines have been added to the file ~/.bash_history:

history -n
ls ~/.bash_history
cat ~/.bash_history
cat ~/.bash_history -a
cls
clear
cat ~/.bash_history -a
cls
clear
cat ~/.bash_history
history -a

2.3 history -c: delete all entries to clear the history list

prupleEndurer @ bash ~ $ history
    1  PS1="prupleEndurer @ bash \w $ "
    2  history 0
    3  echo $?
    4  history
prupleEndurer @ bash ~ $ history -c
prupleEndurer @ bash ~ $ history
    1  history
prupleEndurer @ bash ~ $ 

We first execute the history command to view the current command history list, the content is as follows:

    1  PS1="prupleEndurer @ bash \w $ "
    2  history 0
    3  echo $?
    4  history

Then execute the history -c command to clear the command line history list of the current session.

Then execute the history command again to view the current command history list. At this time, the history list only contains the commands we entered this time:

    1  history

The previous command history list has been cleared.

2.4 history -d offset: delete the history list content from the specified location

prupleEndurer @ bash ~ $ history
    1  PS1="prupleEndurer @ bash \w $ "
    2  history
    3  history 0
    4  echo $?
    5  history 3
    6  history
prupleEndurer @ bash ~ $ history -d 2
prupleEndurer @ bash ~ $ history
    1  PS1="prupleEndurer @ bash \w $ "
    2  history 0
    3  echo $?
    4  history 3
    5  history
    6  history -d 2
    7  history
prupleEndurer @ bash ~ $ 

We first execute the history command to view the current command history list, the content is as follows:

    1  PS1="prupleEndurer @ bash \w $ "
    2  history
    3  history 0
    4  echo $?
    5  history 3
    6  history

Then execute the history -d 2 command to delete the second line of commands

Then execute the history command again to view the current command history list, the content is as follows:

    1  PS1="prupleEndurer @ bash \w $ "
    2  history 0
    3  echo $?
    4  history 3
    5  history
    6  history -d 2
    7  history

You can see the previous 2nd record in the command history list:

    2  history

has been deleted. 

If the offset I specify is a negative number, such as -2, the command will error with an exit status code of 1:

prupleEndurer @ bash ~ $ history -d -2
bash: history: -2: history position out of range
prupleEndurer @ bash ~ $ echo $?
1
prupleEndurer @ bash ~ $ 

2.5 history positive integer: display the specified number of historical command records

 prupleEndurer @ bash ~/Code $ history
    1  history
prupleEndurer @ bash ~/Code $ history 1
    2  history 1
prupleEndurer @ bash ~/Code $ history 0
prupleEndurer @ bash ~/Code $ history 2
    3  history 0
    4  history 2
prupleEndurer @ bash ~/Code $ history
    1  history
    2  history 1
    3  history 0
    4  history 2
    5  history
prupleEndurer @ bash ~/Code $ 

If we specify a negative number, an error will be prompted and the command exit value will be 2.

prupleEndurer @ bash ~ $ history -2
bash: history: -2: invalid option
history: usage: history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...]prupleEndurer @ bash ~ $ echo $?
2

2.6 history -r: Read history files and append the contents to the history list

prupleEndurer @ bash ~ $ history
    1  PS1="prupleEndurer @ bash \w $ "
    2  cat ~/.bash_history
    3  set | grep  $HISTFILE
    4  cat /home/csdn/.bash_history
    5  history -w
    6  cat /home/csdn/.bash_history
    7  history -w c.log
    8  cat c.log
    9  history
prupleEndurer @ bash ~ $ history -r
prupleEndurer @ bash ~ $ history
    1  PS1="prupleEndurer @ bash \w $ "
    2  cat ~/.bash_history
    3  set | grep  $HISTFILE
    4  cat /home/csdn/.bash_history
    5  history -w
    6  cat /home/csdn/.bash_history
    7  history -w c.log
    8  cat c.log
    9  history
   10  history -r
   11  PS1="prupleEndurer @ bash \w $ "
   12  cat ~/.bash_history
   13  set | grep  $HISTFILE
   14  cat /home/csdn/.bash_history
   15  history -w
   16  history
prupleEndurer @ bash ~ $ cat /home/csdn/.bash_history
PS1="prupleEndurer @ bash \w $ "
cat ~/.bash_history
set | grep  $HISTFILE
cat /home/csdn/.bash_history
history -w
prupleEndurer @ bash ~ $ 

We first execute the history command to view the current command history list, which contains 9 items. The content is as follows:

    1  PS1="prupleEndurer @ bash \w $ "
    2  cat ~/.bash_history
    3  set | grep  $HISTFILE
    4  cat /home/csdn/.bash_history
    5  history -w
    6  cat /home/csdn/.bash_history
    7  history -w c.log
    8  cat c.log
    9  history

Then execute the history -r command to read the history file and append the content to the history list

Then execute the history command again to view the current command history list. There are 16 items in total. The content is as follows:

    1  PS1="prupleEndurer @ bash \w $ "
    2  cat ~/.bash_history
    3  set | grep  $HISTFILE
    4  cat /home/csdn/.bash_history
    5  history -w
    6  cat /home/csdn/.bash_history
    7  history -w c.log
    8  cat c.log
    9  history
   10  history -r
   11  PS1="prupleEndurer @ bash \w $ "
   12  cat ~/.bash_history
   13  set | grep  $HISTFILE
   14  cat /home/csdn/.bash_history
   15  history -w
   16  history

We use the command  cat /home/csdn/.bash_history to view the contents of the history file:

PS1="prupleEndurer @ bash \w $ "
cat ~/.bash_history
set | grep  $HISTFILE
cat /home/csdn/.bash_history
history -w

You can see that the 10th to 15th records in the current command history list are added from the history file.

2.7 history -s command: append the specified command to the history list

prupleEndurer @ bash ~/Code $ history
    1  PS1="prupleEndurer @ bash \w $ "
    2  ls
    3  cd Code
    4  pwd
    5  history
    6  pwd
    7  pwd
    8  pwd
    9  set | grep hist
   10  set | grep HIST
   11  export | grep HIST
   12  export
   13  history
prupleEndurer @ bash ~/Code $ history -s abcd
prupleEndurer @ bash ~/Code $ history
    1  PS1="prupleEndurer @ bash \w $ "
    2  ls
    3  cd Code
    4  pwd
    5  history
    6  pwd
    7  pwd
    8  pwd
    9  set | grep hist
   10  set | grep HIST
   11  export | grep HIST
   12  export
   13  history
   14  abcd
   15  history
prupleEndurer @ bash ~/Code $

In the above example, we execute the command  history -s abcd  to add abcd to the command history list.

2.8 history -w [file]: Write the current history into the history file and append it to the history list

2.8.1 Write the current history to the default history file

The system default history file specifier is specified by the environment variable HISTFILE.

prupleEndurer @ bash ~ $ set | grep  $HISTFILE
HISTFILE=/home/csdn/.bash_history
_=/home/csdn/.bash_history
prupleEndurer @ bash ~ $ cat /home/csdn/.bash_history
cat: /home/csdn/.bash_history: No such file or directory
prupleEndurer @ bash ~ $ history -w
prupleEndurer @ bash ~ $ cat /home/csdn/.bash_history
PS1="prupleEndurer @ bash \w $ "
cat ~/.bash_history
set | grep  $HISTFILE
cat /home/csdn/.bash_history
history -w
prupleEndurer @ bash ~ $ 

We first execute  the set | grep $HISTFILE  command to query the value of the environment variable HISTFILE: /home/csdn/.bash_history.

Then we execute  the cat /home/csdn/.bash_history  command to view the contents of /home/csdn/.bash_history. As a result, this file does not exist.

Then we execute  the history -w  command to save the command line history to /home/csdn/.bash_history.

Finally, we execute the cat /home/csdn/.bash_history  command again to view the contents of /home/csdn/.bash_history. This time the file not only exists, but the content of the file is the command we entered before.

2.8.2 Write the current history to the specified file

We save the command line history to the file c.log in the current directory.

prupleEndurer @ bash ~ $ history -w c.log
prupleEndurer @ bash ~ $ cat c.log
PS1="prupleEndurer @ bash \w $ "
cat ~/.bash_history
set | grep  $HISTFILE
cat /home/csdn/.bash_history
history -w
cat /home/csdn/.bash_history
history -w c.log
prupleEndurer @ bash ~ $ 

  

First, we execute the command  history -w c.log    to save the command line history to the file c.log in the current directory.

Then, we execute the command  cat c.log    to view the contents of the file c.log.

2.9 !: Repeat the command

2.9.1 !! or !-1: Repeat the previous command

prupleEndurer @ bash ~/Code $ pwd
/home/csdn/Code
prupleEndurer @ bash ~/Code $ !!
pwd
/home/csdn/Code
prupleEndurer @ bash ~/Code $ !-1
pwd
/home/csdn/Code
prupleEndurer @ bash ~/Code $ 

 

When we execute the command !! or !-1, the previous command pwd will be executed repeatedly.

In addition to the above two methods, there are two other methods:

  • Use the arrow keys ↑ to select the command, press ↑↓ to adjust, and press Enter to execute.
  • Press Ctrl + p to call up the command and press Enter to execute it

 2.9.2 !Command history list column number: Repeat the command of the specified line in the history list

prupleEndurer @ bash ~ $ ls
Code
prupleEndurer @ bash ~ $ ls Code
prupleEndurer @ bash ~ $ cd Code
prupleEndurer @ bash ~/Code $ ls
prupleEndurer @ bash ~/Code $ history
    1  PS1="prupleEndurer @ bash \w $ "
    2  ls
    3  ls Code
    4  cd Code
    5  ls
    6  history
prupleEndurer @ bash ~/Code $ !2
ls
prupleEndurer @ bash ~/Code $ ! 2
bash: 2: command not found

In the above example, the command !2 will execute the 2nd command ls in the command history list

Since there is no content in the current directory, the ls command is not displayed.

If the command we enter is ! 2, an error will be reported.

Notice:

  • There cannot be a space between ! and the command history list column number.
  • If the column number of the command history list is a positive integer, count from the beginning to the bottom; if it is a negative integer, count from the bottom to the top. For example, !-1 executes the last command in the command list.

What happens if the command history list column number we specify is not in the actual number of columns in the command history list?

prupleEndurer @ bash ~/Code $ !0
bash: !0: event not found
prupleEndurer @ bash ~/Code $ echo $?
0
prupleEndurer @ bash ~/Code $ !100
bash: !100: event not found
prupleEndurer @ bash ~/Code $ echo $?
0
prupleEndurer @ bash ~/Code $ 

When we execute the commands !0 and !100, the system will prompt: event not found

The command return values ​​are all 0.

 2.9.3 !String: Repeat the command starting with the specified string in the history list

prupleEndurer @ bash ~/Code $ history
    1  PS1="prupleEndurer @ bash \w $ "
    2  ls
    3  ls Code
    4  cd Code
    5  ls
    6  history
    7  ls
    8  ! 2
    9  cd Code
   10  ehco $?
   11  echo $?
   12  echo $?
   13  echo $?
   14  echo $?
   15  echo $?
   16  history
prupleEndurer @ bash ~/Code $ !cd
cd Code
bash: cd: Code: No such file or directory
prupleEndurer @ bash ~/Code $ ! cd
prupleEndurer @ bash ~ $ !ls
ls
Code
prupleEndurer @ bash ~ $ !ls Cod
ls Cod
ls: cannot access Cod: No such file or directory
prupleEndurer @ bash ~ $ echo $?
2
prupleEndurer @ bash ~ $ 

We execute the command history to view the current command history list

Then the !cd , ! cd , ​​!ls  , !ls Cod commands were executed successively   . From the implementation point of view, two points should be noted:

  1. There cannot be a space between ! and string
  2. If the command history list does not include the command with the specified string, the system will directly execute the string, such as  !ls Cod

2.10 Other usage tips

You can try some of the usage techniques introduced online.

2.10.1 Re-execute or view commands in command history

  • !:0 Remove the parameters from the previous command and then execute it
  • Ctrl+n displays the next command in the current history but does not execute it
  • Ctrl + j executes the current command
  • !?string Repeat the previous command containing string string
  • !string:p only prints the command history without executing it
  • !$:p prints the contents of !$ (the last parameter of the previous command)
  • !*:p prints the contents of !* (all parameters of the previous command)
  • ^string deletes the first string in the previous command
  • ^string1^string2 replaces the first string1 in the previous command with string2
  • !:gs/string1/srting2 Replace all string1 in the previous command with string2

2.10.2 Using (reverse-i-search) mode

Crtl + r: Search command in command history

Crtl + g: Exit from history search mode

2.10.3 Using parameters from the command history

  •  Recall the last parameter in the previous command

     There are 3 methods:

  1. !$
  2. Esc . (Click and release the Esc key, then click the . key)
  3. Alt+. (Hold down the Alt key and click the . key). The Alt function key is blocked in some terminal software and needs to be turned on.
  • command !^ uses the first parameter of the previous command as the parameter of cmd
  • command !$ uses the last parameter of the previous command as the parameter of cmd
  • command !* uses all parameters of the previous command as cmd parameters
  • command !:n uses the nth parameter of the previous command as the parameter of cmd
  • command !n:^ calls the first parameter of the nth command
  • command !n:$ calls the last parameter of the nth command
  • command !n:m calls the m-th parameter of the n-th command
  • command !n:* calls all parameters of the nth command
  • command !srting:^ Search the command history for the command starting with string and get its first parameter
  • command !srting:$ Search the command history for the command starting with string and get its last parameter
  • command !srting:n Search the command history for the command starting with string and get its nth parameter
  • command !srting:* Search the command history for the command starting with string and get all its parameters

3. Environment variables related to the history command

prupleEndurer @ bash ~/Code $ set | grep HIST
HISTFILE=/home/csdn/.bash_history
HISTFILESIZE=500
HISTSIZE=500

  • HISTFILE: Default command history file specifier specified
  • HISTFILESIZE: Specifies the total number of records of commands saved in the default command history file, that is, there are at most HISTFILESIZE lines in the command history file.
  • HISTSIZE: Specifies the number of records output by the history command, that is, the last HISTSIZE line in the output command history file

おすすめ

転載: blog.csdn.net/Purpleendurer/article/details/135184751