History no és una comanda que disposi d'un executable. Si feu:
$ which history
No tornar res. I si intenteu buscar el paquet que instal·la la comanda, tampoc el trobareu
$ dpkg -S history | grep bin consolekit: /usr/bin/ck-history
Igual que altres comandes com la comanda cd, la comanda history és una built-in-command i forma part de la shell (normalment bash).
Per aquesta raó quan executeu:
$ man history
Tampoc apareix l'ajuda de la comanda, sinó que apareix l'ajuda d'una llibreria del mateix nom!
L'ajuda de la comanda history està integrada en bash. Cal consultar el manual de bash:
$ man bash
Per exemple podeu consultar la secció:
Commands for Manipulating the History
o la secció
HISTORY When the -o history option to the set builtin is enabled, the shell provides access to the command history, the list of commands previously typed. The value of the HISTSIZE variable is used as the number of commands to save in a history list. The text of the last HISTSIZE commands (default 500) is saved. The shell stores each command in the history list prior to parameter and variable expansion (see EXPANSION above) but after history expansion is performed, subject to the values of the shell variables HISTIGNORE and HISTCONTROL. On startup, the history is initialized from the file named by the variable HISTFILE (default ~/.bash_history). The file named by the value of HISTFILE is truncated, if necessary, to contain no more than the number of lines specified by the value of HISTFILESIZE. When an interactive shell exits, the last $HISTSIZE lines are copied from the history list to $HISTFILE. If the histappend shell option is enabled (see the descrip‐ tion of shopt under SHELL BUILTIN COMMANDS below), the lines are appended to the history file, otherwise the history file is overwritten. If HISTFILE is unset, or if the history file is unwritable, the history is not saved. After saving the history, the history file is truncated to contain no more than HISTFILESIZE lines. If HISTFILESIZE is not set, no truncation is performed. The builtin command fc (see SHELL BUILTIN COMMANDS below) may be used to list or edit and re-execute a portion of the history list. The history builtin may be used to display or modify the history list and manipulate the history file. When using command-line editing, search commands are available in each editing mode that provide access to the history list. The shell allows control over which commands are saved on the history list. The HISTCONTROL and HISTIGNORE variables may be set to cause the shell to save only a subset of the commands entered. The cmdhist shell option, if enabled, causes the shell to attempt to save each line of a multi-line command in the same history entry, adding semicolons where necessary to preserve syntactic correctness. The lithist shell option causes the shell to save the command with embedded newlines instead of semicolons. See the description of the shopt builtin below under SHELL BUILTIN COMMANDS for information on setting and unsetting shell options.
Cada cop que s'inicialitza una shell de bash s'utilitza aquest fitxer per omplir la historia.
Aquest fitxer es trunca a la mida de la variable HISTFILESIZE (que per defecte és 500).
El que mostra la comanda history i el que hi ha al fitxer ~/.bash_history no està sempre sincronitzat! El fitxer és llegeix al iniciar una nova terminal (sessió bash), però no es torna a escriure les comandes d'aquesta sessió, fins que no es surt de la terminal (per exemple amb la comanda exit).
És després de sortir que s'afegeixen les comandes executades a la historia.
La mida del fitxer acostuma a ser 500:
$ cat .bash_history | wc -l 500
Que és el valor per defecte de la variable HISTFILESIZE
NOTA: Observeu que les variables HISTFILESIZE i HISTSIZE no tenen per què ser iguals! HISTSIZE és la mida de la historia a la línia de comandes!
Es pot forçar la sincronitcació de la comanda history al fitxer .bash_history amb:
$ history -a
També es pot forçar que la nostra terminal llegeixi els continguts del fitxer .bash_history amb:
$ history -n
Cal tenir en compte que si una terminal mor de forma inesperada, no es podrà fer el volcat de les comandes executades en aquella sessió.
$ history 5 507 joe drblpush.diff 508 ls 509 history 1 510 history 2 511 history 5
Cal establir la variable:
HISTTIMEFORMAT
Per exemple:
$ export HISTTIMEFORMAT="%F %T "
I després:
$ history
NOTA: no és retroactiu!
Cal afegir les línies:
HISTSIZE=10000 HISTFILESIZE=10000 HISTTIMEFORMAT="%F %T "
Al fitxer /etc/skel/.bashrc
Ho determina la variable:
HISTSIZE
La podeu consultar (si la teniu establerta) amb:
$ env | grep HISTSIZE
El valor per defecte és 500. La podeu establir al vostre perfil (fitxer .bashrc).
No confondre amb la variable:
HISTFILESIZE
Que determina el número de línies del fitxer ~/.bash_history!
Del manual:
-d offset Delete the history entry at position offset.
$ history -d 34
$ history -c
Es pot afegir al fitxer ~/.bashrc les línies:
HISTFILESIZE=0 HISTSIZE=0
$ shopt -s histappend [email protected]:/usr/share/man$ shopt | grep hist cmdhist on histappend on histreedit off histverify off lithist off
!!--> Última ordre !n--> Executa l'ordre n de la història d'ordres. !-n--> Executa l'ordre -n !paraula--> Executa la última ordre que contenia la paraula indicada. !?paraula --> Ctrl+P ---> Executa la última ordre.
Es pot utilitzar la variable PROMPT_COMMAND. Aquesta variable conté la comanda o comandes que s'executen cada cop que es mostra un prompt a la línia de comandes.
Si afegim al fitxer ~/.bashrc les línies:
export PROMPT_COMMAND="history -n; history -a"
Forçarem la sincronització cada cop que executem una comanda!
Ho podem forçar per a tots els nous usuaris editant el fitxer /etc/skel/.bashrc:
Recursos:
Al manual de bash s'anomenen Event Designators a les referències de les comandes de la llista history.
Les opcions són
! Start a history substitution, except when followed by a blank, newline, carriage return, = or ( (when the extglob shell option is enabled using the shopt builtin). !n Refer to command line n. !-n Refer to the current command line minus n. !! Refer to the previous command. This is a synonym for ‘!-1’. !string Refer to the most recent command starting with string. !?string[?] Refer to the most recent command containing string. The trailing ? may be omitted if string is followed immediately by a newline. !# The entire command line typed so far.
Podeu utilitzar la combinació de tecles Ctrl-r, per tal de buscar comandes al history.
Consulteu:
LPI_103.1._Treballar_a_la_línia_d'ordres#Ctrl.2Br
Podeu mostrar la comanda anterior amb Ctrl-p.
export HISTCONTROL=ignoredups
env | grep HIST HISTCONTROL=ignoreboth
Només cal posar un espai davant:
$ ls -la
Sempre i quan estigui el paràmetre ignorespace activat
# export HISTCONTROL=ignorespace
Cal utilitzar HISTIGNORE:
# export HISTIGNORE=”pwd:ls:ls -ltr:”