Answer the question
In order to leave comments, you need to log in
How does command history work in bash?
There are several sub-questions:
1) how are the entered commands in bash recorded in the command history (.bash_history)? (I am interested in the recording algorithm)
2) is it possible to write the entered commands into 2 files? (how to configure bashrc for this)
3) is it possible after entering the command send its text (not the result of execution, but the command itself) via a script to email?
Answer the question
In order to leave comments, you need to log in
1) on exit, bash simply writes all executable commands to the $HISTFILE file (by default it is ~/.bash_history.
Before exiting, the history is stored in memory, and the HISTSIZE and HISTFILESIZE parameters can be different.
You can reset the current history from memory to a file with the history command -a
2), 3). you can try to add the necessary commands to the prompt, then when you execute any command, the block of your commands will be executed. But this is a somewhat hacky solution. For example, add history -a to PROMPT_COMMAND so that the histfile is updated after each command. you can add the output of history 1 there with redirection to the script that sends the mail, but this will cause a delay in the execution of each command. it is better to monitor this file from a separate session.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question