History and it relevant variables in Linux/gnu and Mac OS History command and related environment variables

Source: Internet
Author: User
Tags repetition

For Terminalor, the history command is not unfamiliar, what!n,!! is more commonly used, and the cmds you hit at the command line is saved by default in/home/$USER/.bash_history (Linux)

/users/$USER/.bash_history (MAC).

But history doesn't just look like this: You save the command and then you can see the call.

Introduce some of the history-related variables to control the history behavior and size

Histignore (Hist-ignore) tells the history which commands do not need to be saved and ignores them directly .

For example mine is:

histignore= "ls *:ll:ls:l:ll *:freq*:cd *:*. log:dl:dc:des:bi:pic:sublime:brackets:open*"

is the common LS command to ignore, these are very common but rarely be recycled "scrap", CD to where no use, there is no use to view the log file, freq* is my common directory alias

For example alias freq1= "CD ~/edge/dl/gse38600/new"

There are also DL dc des pic bi corresponding to an alias, alias dl= "CD ~/downloads" such as

and sublime brackets is my favorite editor, in the Mac is alias sublime= "open-a sublime\ text", Linux directly with the SUBL is good

Open* is also under the Mac

For Linux systems in the Debian class, the common Apt-get Class command can be alias

Alias update= "sudo apt-get update"

Alias upgrade= "sudo apt-get upgrade | Tee ~/documents/' date +%f_%h:%m:%s '-upgrade.log "

Alias dist-upgrade= "sudo apt-get dist-upgrade | Tee ~/documents/' date +%f_%h:%m:%s '-dupgrade.log "

Alias remove= "sudo apt-get remove"

Alias install= "sudo apt-get install"

Alias purge= "sudo apt-get purge"

...

and add Update,upgrade,dist-upgrade to the Histignore.

Histsize indicates the maximum number of commands that can be saved, which you can see by default. BASHRC or bash_profile. This variable is generally defined as 1000

Exceeding this number will automatically squeeze out the original command, which is equivalent to a tunnel.

But really not enough to use, I defined histsize as 1,000,000 anyway, not to meet this requirement, general 100,000 also takes a long time to reach

Generally can file to a certain size to backup it, and then restart a file save, backup can write a bash function and combined with history to view the old and new files

Histfilesize represents the maximum size of the file, the default is 1000, self-according to their actual cmd amount, DIY

Histtimeformat (Hist-time-format) is of course the time to record the command you knocked on, the format and the date are the same

Mine is defined as:

histtimeformat= "%f#%h:%m:%s" >>>>> 2015-04-08#04:27:32

histtimeformat= "%f#%t" >>>>> 2015-04-08#04:27:32

[Note that this time will not be stored in the. Bash_history, just show it when you use history to view]

Histcontrol is the equivalent of. Bash_history quality control, with ignoredups,erasedups (Ignore duplicates, and erase duplicates)

If chooses Ignoreredups, is ignores the continuous repetition, does not have the function continuously.

If you are debugging a program, the Perl bowtie2_all.pl, if you try to succeed 20 times, but there are 20 commands in history,

Of course you can ignore this command histignore, but it's unwise, histcontrol, it will check if the command to save is the same as when it is deposited

If you repeat, you ignore it, that is, do not deposit

If you choose Erasedups, of course, in the deposit of the order to be deposited and all subsequent orders than the first, if there is a repetition, will not deposit this order

There is also a trick: you can choose Ignorespace, meaning that when you enter the command, if you have a space at the beginning, then you will not deposit this command , so

You can ignore it by adding one or more spaces to the command you don't want to deposit.

Histfile, of course, is to customize where you want to save the command, the default is ~/.bash_history, if you do not want to save here, specify the place to save, OK.

For example histfile= "/home/$USER/backup/bash_2015_history"

[Note best use absolute path]

Note that the definitions of these variables are stored in the. BASHRC and then the command line source ~/.BASHRC, It means to change the settings in the. BASHRC to overwrite the settings that have been read into memory (the terminal that are opened in the future will use this setting, and the original settings are maintained in addition to this window)

It's annoying to use export to import, because this setting doesn't work after you turn off the terminal or reboot, because every time you open the terminal it will be reread. BASHRC

Of course, I write the export variable=content directly. BASHRC not good, but directly with the variable=content is good, why hit 7 more characters?

What's more, all the commands you knock at this window will be written after the window exit or close. Bash_history, (caching mechanism), if you want to hit a write a word, hehe, to a trick

Prompt_command= "HISTORY-A; $PROMPT _command "

Prompt_command is the BASH_RC defined in the PS1, that is, every time the [email protected] : ~ $, the above trick is in each PROMPT before the order is deposited,- A can specify a different file name,

But the default is. bash_profile

Since the bash history file is overwritten by default, if there are multiple terminals, the last exit overwrites the previous record, instead of the Append form:shopt-s histappend, but the default is in. BASHRC, no need to fill in

History N View the most recently deposited n command: equivalent to history | Tail-n N

! n View and run the saved nth command: History | Sed-n ' Np ' (View only)

!! View and run the previous command

! STR view commands that begin with Str

But I prefer: Alias hist= "History | grep "

-w/-r/-C are write To/read from/clear respectively

Ctrl + R is also a popular history search

Reference:

1. http://www.jb51.net/LINUXjishu/157030.html

2. http://blog.csdn.net/dyllove98/article/details/9412805

History and it relevant variables in Linux/gnu and Mac OS History command and related environment variables

Related Article

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.