Storage and record of Bash command history

Source: Internet
Author: User
In Bash, we can use the history command to review, modify, and reuse previous historical commands. Remove the signal mechanism, remove the job control, and remove various parameter calls. Today, we only want to see how to record commands in Bash and how to store historical commands.

In Bash, we can use the history command to review, modify, and reuse previous historical commands. Remove the signal mechanism, remove the job control, and remove various parameter calls. Today, we only want to see how to record commands in Bash and how to store historical commands.

In the source code of Bash, the definition code of the history Command is builtins/history. def and builtins Directory stores the source code of internal commands. each internal command is a def file, such as history. def, cd. def. In Makefile, DEFSRC declares the def files of all internal commands. Mkbuiltins. c is used to generate the auxiliary tool mkbuiltins during compilation. mkbuiltins processes *. def files and generates the *. c source program of the command and builtins. c and builtext. h. Builtins. c and builtext. h are equivalent to the indexes of each internal command.

History. def parses commands and distributes different parameters to different functions, such as reading and overwriting commands. Some function implementation code files are stored in bashhist. c/bashhist. h, but the basic operations on history are not included in these two files, they are in lib/readline. This is because Bash uses the GNU Readline function library. Readline provides a unified command line interaction mode for row editing and history functions.

The history command displays all historical records. all historical records include the records first read from the file and the records generated by the current session. Assume that you already have 500 commands in your history. if you use another document editor to write 1000 historical log files and open the terminal, you will find that only 500 records are displayed. This is because when the terminal is opened for initialization, there will be not only read operations on the history list, but also initialization operations on the number of file records limit to ensure that the number of records in the file will not exceed the set maximum value. This initialization operation is implemented in the load_history function. at the beginning, the function intercepts the history file twice. the default value is 500, and the maximum value is HISTFILESIZE.

There is a session concept in the history. commands in different sessions do not conflict with each other before they are saved to a file. For example, open Terminal A if you delete it. save the first 10 lines of bash_history and input history in the command line. you will find that the output command history does not start from 1, but starts from 11. If you open Terminal B, enter several commands, and then input history, you will find that the commands entered in Terminal A are not in the history. This is because in a terminal session, there is only one read operation for the historical record from the fixed storage location, and only one write operation, that is, reading when the terminal is opened and writing when the terminal is closed.

In addition to file storage, history can also be recorded in MMAP. the corresponding macro is defined as HISTORY_USE_MMAP.

If it is stored as a file, the command record is stored in. bash_history (by default, if HISTFILE is set, the value in HISTFILE is preferentially used). although we can see that there is a label before each command when using the history Command, we can use it! The label method re-executes the command. This label is not unique. it is marked in the program operation when the file is read during initialization, the number is auto-incrementing. this auto-incrementing value is not limited by the maximum value of historical records.

When a session exits, the history file is saved, historical data is saved, and session statistics are cleared. When files are stored, Bash stores commands in previous sessions directly to the end of the file. if the number of records in the file exceeds the defined maximum number of records, the old history commands are cleared, in addition, the previous files will be overwritten when the storage is resumed. The sample code is as follows:

      if (history_lines_this_session <= where_history () || force_append_history) append_history (history_lines_this_session, hf); else write_history (hf);        sv_histsize ("HISTFILESIZE");

Storage operations are ultimately stored in the read and write operations on the storage media. for example, the read and write operations on files only impose various restrictions on business logic rules. The command can be recorded when the command is executed, or when the command is just entered but identified, Bash belongs to the latter. Bash uses the maybe_add_history function to write user-input commands in syntax analysis of yacc to the command history table of the current session. The command entered by the user has been recorded during syntax analysis. in this case, you do not have to worry about the final result of the command, or how to handle exceptions in the execution process. It only records the commands entered by the user before execution. Therefore, we can define the Bash command history as the command history.

References: http://files.linjian.org/articles/techreport/bash_study.tar.gz

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.