Difference between revisions of "Bash history"

From wikieduonline
Jump to navigation Jump to search
 
(34 intermediate revisions by 3 users not shown)
Line 1: Line 1:
Bash Command history feature allows to recall, edit and rerun previous commands. Also allows commands to be saved using the <code>history -a</code> command<ref>https://unix.stackexchange.com/questions/145250/where-is-bashs-history-stored</ref><ref>https://www.gnu.org/software/bash/manual/bash.html#Bash-History-Builtins</ref>.
+
[[Bash]] command history feature allows to recall, edit and rerun previous commands. Also allows commands to be saved using the <code>history -a</code> command<ref>https://unix.stackexchange.com/questions/145250/where-is-bashs-history-stored</ref><ref>https://www.gnu.org/software/bash/manual/bash.html#Bash-History-Builtins</ref>.
  
 
Variables to control Bash History:
 
Variables to control Bash History:
* HISTFILE<ref><https://www.gnu.org/software/bash/manual/bash.html#index-HISTFILE</ref>: The default value is ~/.bash_history
+
* [[HISTSIZE]] (<code>[[echo]] $HISTSIZE</code>)
 +
* [[HISTFILE]]<ref><https://www.gnu.org/software/bash/manual/bash.html#index-HISTFILE</ref>: The default value is <code>~/[[.bash_history]]</code>
 
* HISTFILESIZE
 
* HISTFILESIZE
* HISTTIMEFORMAT<ref>https://www.gnu.org/software/bash/manual/bash.html#index-HISTTIMEFORMAT</ref>
+
* [[HISTTIMEFORMAT]] <ref>https://www.gnu.org/software/bash/manual/bash.html#index-HISTTIMEFORMAT</ref>
  
=== Usage ===
+
== Usage ==
* Basic example:
+
* Save every executed command to: <code>[[~/.bash_history]]</code>
:<code>export PROMPT_COMMAND='history -a'</code>
+
:<code>[[export]] [[PROMPT_COMMAND]]='[[history -a]]'</code>
 
* Advanced examples:  
 
* Advanced examples:  
**<code>echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bash_profile</code><ref>https://www.cyberciti.biz/faq/unix-linux-bash-history-display-date-time/</ref>
+
** <code>echo '[[export]] [[HISTTIMEFORMAT]]="%d/%m/%y %T "' >> ~/.[[bash_profile]]</code><ref>https://www.cyberciti.biz/faq/unix-linux-bash-history-display-date-time/</ref>
**<code>export PROMPT_COMMAND='if [ "$(id -u)" -ne 0 ]; then echo "$(date "+%Y-%m-%d.%H:%M:%S") $(pwd) $(history 1)" >> ~/.logs/bash-history-$(date "+%Y-%m-%d").log; fi'</code><ref>https://gist.github.com/NISH1001/bf2b713418b4e2ede8e6a7373b42c4c1</ref>
+
**<code>export [[PROMPT_COMMAND]]='if [ "$(id -u)" -ne 0 ]; then echo "$(date "+%Y-%m-%d.%H:%M:%S") $(pwd) $(history 1)" >> ~/.logs/bash-history-$(date "+%Y-%m-%d").log; fi'</code><ref>https://gist.github.com/NISH1001/bf2b713418b4e2ede8e6a7373b42c4c1</ref>
 
**Sending history to syslog (needs recompiling source): Bash also support syslog capabilities at compiling time<ref>https://unix.stackexchange.com/questions/457107/sending-bash-history-to-syslog</ref> but it is not enabled by default in most distributions: https://github.com/bminor/bash/blob/d233b485e83c3a784b803fb894280773f16f2deb/config-top.h
 
**Sending history to syslog (needs recompiling source): Bash also support syslog capabilities at compiling time<ref>https://unix.stackexchange.com/questions/457107/sending-bash-history-to-syslog</ref> but it is not enabled by default in most distributions: https://github.com/bminor/bash/blob/d233b485e83c3a784b803fb894280773f16f2deb/config-top.h
 
  
 
== Activities ==
 
== Activities ==
 
# Install https://github.com/ohmybash bash customization
 
# Install https://github.com/ohmybash bash customization
 +
# Review output from <code>history</code> command
 +
 +
== Other terms  ==
 +
* <code>[[set -o history]]</code>
 +
* <code>[[docker history]]</code>
 +
* <code>[[history --help]]</code>
 +
* <code>[[histappend]]</code>
  
 
== See also ==
 
== See also ==
* <code>[[acct]]</code>
+
* {{acct}}
 
+
* {{history}}
 +
* {{Bash}}
  
  
Line 28: Line 36:
 
[[Category:Bash programming|*]]
 
[[Category:Bash programming|*]]
  
{{CC License}}
+
{{CC license}}
 
Source: https://en.wikiversity.org/wiki/Bash_programming/Bash_History
 
Source: https://en.wikiversity.org/wiki/Bash_programming/Bash_History

Latest revision as of 14:33, 25 January 2024

Bash command history feature allows to recall, edit and rerun previous commands. Also allows commands to be saved using the history -a command[1][2].

Variables to control Bash History:

Usage[edit]

export PROMPT_COMMAND='history -a'

Activities[edit]

  1. Install https://github.com/ohmybash bash customization
  2. Review output from history command

Other terms[edit]

See also[edit]

Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy.

Source: https://en.wikiversity.org/wiki/Bash_programming/Bash_History

Advertising: