Linux shells like bash have a handy manner of remembering instructions that you just kind, making it straightforward to run them once more with out having to retype them. Simply use the historical past command (which is a bash built-in) after which use an exclamation level adopted by the quantity proven in entrance of the command within the historical past command output that you just need to rerun. Alternatively, you possibly can again as much as that command by urgent the up arrow key as many instances as wanted to succeed in that command after which press return. Don’t neglect, although, that you would be able to additionally arrange instructions you might be doubtless to make use of usually as aliases by including a line like this to your ~/.bashrc file so that you just don’t must seek for them in your command historical past. Right here’s an instance:
$ alias customers="who | awk '{print $1}'" $ customers myacct shs shs $ alias uusers="who | awk '{print $1}' | uniq"
The aliases above will show logged in customers. The second removes duplicates. Add the instructions to your ~/.bashrc file to have them arrange every time you log in.
Making use of the historical past command could make it simpler to reuse instructions and keep away from typos. Nonetheless, there are methods to get Linux to disregard among the instructions that you just kind in order that your historical past buffer doesn’t refill with instructions you don’t need it to recollect – particularly if these instructions would take up a whole lot of room in your historical past buffer.
Command historical past may assist with troubleshooting an issue as you may get an inventory of the instructions that have been run earlier than the issue appeared.
How a lot to recollect
The $HISTSIZE setting controls what number of instructions will probably be remembered. Should you solely need the newest 100 strains to be remembered in your historical past buffer, you might run a command like this:
$ export HISTSIZE=100
Typically, $HISTSIZE defaults to 1,000 instructions. Examine it by operating a command like this one:
$ echo $HISTSIZE 1000
Change the setting in your ~/.bashrc file if you would like historical past to recollect extra or fewer instructions.
Ignoring particular instructions
You’ll be able to hold sure instructions from being saved in your historical past buffer by including them to the $HISTIGNORE variable. Within the setting proven under, the pwd, date, historical past and clear instructions will probably be ignored since these instructions are unlikely to be rerun utilizing command historical past.
$ export HISTIGNORE='pwd:date:historical past:clear' $ pwd /house/myacct $ historical past $ historical past | tail -1 86 pwd 87 historical past | tail -1
Be aware, nonetheless, that the historical past | tail -1 command proven above is captured, however not the historical past command when entered by itself.
Ignoring duplicate instructions
Should you kind a command some variety of instances in a row and your $HISTCONTROL setting incorporates the string “ignoredups” (i.e., ignore duplicates), you’ll solely see the command as soon as for every repetitive sequence.
$ echo hey hey $ pwd /house/myacct $ pwd /house/myacct $ who | wc -l 4 $ pwd $ historical past | tail -5 79 echo hey 80 pwd <== was run twice, however is proven as soon as 81 who | wc -l 82 pwd 83 historical past | tail -6
Ignoring instructions which are entered with a previous house
You may as well elect to have your historical past buffer put out of your mind any command that you just enter whenever you run the command after urgent the house key – for instance, for those who kind “ date” as a substitute of “date”. This lets you have your command historical past ignore any command with out having to arrange every command individually in your $HISTIGNORE setting.
The truth is, if you would like your command historical past to omit each instructions that begin with an area together with duplicate instructions entered sequentially, both of those settings will do the identical factor. Add them to your ~/.bashrc file to make them everlasting.
$ export HISTCONTROL="ignoredups:ignorespace" $ export HISTCONTROL="ignoreboth"
To be clear, the “ignoreboth” setting means to disregard each duplicate instructions when entered sequentially and instructions which are entered with a previous house.
Your historical past file
The command historical past file is recognized by the $HISTFILE setting, however is never something however .bash_history. Be aware that instructions you simply entered won’t but have been added to this file.
$ echo $HISTFILE /house/shs/.bash_history
The command under could be added to my historical past buffer, however word that it doesn’t present up once I ask to see the underside of my .bash_history file.
$ echo byebye for now byebye for now $ tail -1 ~/.bash_history tail -2 ~/.bash_history
However on subsequent login, I’ll see this:
$ tail -1 ~/.bash_history echo byebye for now
Wrap-up
Deciding what instructions you need remembered in your command historical past file and which you need ignored can enhance the usefulness of your command historical past – particularly for those who don’t need to scan via lots of of remembered instructions to seek out the handful that you just need to return. I hope that some a part of this put up has left you with some good concepts about making your command historical past extra worthwhile.
Copyright © 2023 IDG Communications, Inc.