Bash history without line numbers
Try this:
$ history | cut -c 8-
How to suppress numbers in Bash history?
man bash
I have emphasized the key phrase here
HISTTIMEFORMAT
If this variable is set and not null, its value is used as a format string for strftime(3) to print the time stamp associated with each history entry displayed by the history builtin. If this variable is set, time stamps are written to the history file so they may be preserved across shell sessions.
delete specific line numbers from history
In bash:
for offset in $(history | awk '/blink/ {print $1}' | tac)
do
history -d $offset
done
You can get the offsets directly from the history
command, no need to generate line numbers with grep
. Also you need to delete the lines in reverse (hence use of tac
), because the offset of the commands following the one being deleted are shifted down.
how do I limit the number of history items in bash?
This is controlled by the HISTSIZE and HISTFILESIZE built-in shell variables. Quoting from the documentation in man bash
:
HISTSIZE
- The number of commands to remember in the command history (see HISTORY below). If the value is 0, commands are not saved in the history list. Numeric values less than zero result in every command
being saved on the history list (there is no limit). The shell sets the default value to 500 after reading any startup files.
HISTFILESIZE
- The maximum number of lines contained in the history file. When this variable is assigned a value, the history file is truncated, if necessary, to contain no more than that number of lines by removing
the oldest entries. The history file is also truncated to this size after writing it when a shell exits. If the value is 0, the history file is truncated to zero size. Non-numeric values and numeric
values less than zero inhibit truncation. The shell sets the default value to the value of HISTSIZE after reading any startup files.
~/.bashrc
is an appropriate place to set these values. They don't need to be exported, since they're internal to the shell:
HISTFILESIZE=25
...will do fine, no export
needed.
Why some lines in Bash history become * ?
The star means the line has been modified. See man history
.
Demonstration:
From the terminal prompt use the up-arrow key to display a previous command.
Delete the command with the backspace key.
Use the down-arrow key to return to the last prompt and enter
history
The modified line should now be displayed in the history as a line number followed by a star.
How do I select a subset of lines from the history in bash?
grep's -C option provides context. Try:
$ history | grep -C 5 ifconfig
zsh `history n` displays the first n entries instead of the last n entries
The documentation for zsh history
, in zshbuiltins(1), is:
history
Same as
fc -l
.
where fc -l
is:
fc -l [ -LI ] [ -nrdfEiD ] [ -t timefmt ] [ -m match ] [ old=new ... ] [ first [ last ] ]
If
first
is not specified, it will be set to -1 (the most recent event), or to -16 if the-l
flag is given. Iflast
is not specified, it will be set tofirst
, or to -1 if the-l
flag is given. However, if the current event has added entries to the history withprint -s
orfc -R
, then the defaultlast
for-l
includes all new history entries since the current event began.When the
-l
flag is given, the resulting events are listed on standard output.
so run:
history -<n>
with a hyphen-minus, as in history -16
, to see the last n lines of history in zsh.
How to delete history in a range in linux bash
for ((i=258;i<=262;++i)); do "history -d $i"; done;
as you said, is simply not working due to quotes
for i in {5..10}; do history -d $i; done;
is working, but you must ensure to have entries from 5 to 10, which in your examples, you don't have.
Take into account that every item you remove, the position indexes of the other items scale down of one.
Basically you should reverse loop items:
for i in {10..5}; do history -d $i; done;
to not be affected by the position index changes while looping.
Edit
As Cyrus suggested, there's another approach.
You want to remove items from 258 to 262, a total of 5 entries (262 is included)
You can delete 5 times the entry at position 258.
Taking advantage of the index scaling down of one every time you delete an item:
- the first time, you delete the item 258
- the second time, you delete the ex-item 259, that has scaled down to 258
- the third time, you delete the ex-ex-item 260, that has scaled down to 259 (in the first delete) and to 258 (in the second delete)
- the fourth time, you delete the ex-ex-ex-item 261, that has scaled down to 260 (in the first delete), to 259 (in the second delete) and to 258 (in the third delete)
- the fifth time, you delete the ex-ex-ex-ex-item 262...
How do I delete lines from my bash history matching a specific pattern?
Quick answer:
while read n; do history -d $n; done < <(history | tac | awk '/function/{print $1}')
Explanation:
The history
command accepts only a single offset when using the -d
flag. On top of that when you delete an entry, it also renumbers all the commands after this entry. For this reason we revert the output of history
using tac
and process the lines from last to first. This short awk
line just replaces the grep and sed command to pick up the history offset.
We do not use a full pipeline as this creates subshells and history -d $n
would not work properly. This is nicely explained in: Why can't I delete multiple entries from bash history with this loop
Note: If you want to push this to your history file ($HISTFILE
), you have to use history -w
Warning: When you have multiline commands in your history the story becomes very complicated and strongly depends on various options that have been set. See [U&L] When is a multiline history entry (aka lithist) in bash possible? for the nasty bits.
Related Topics
Escaping Quotes When Using Ssh
Git Diff with Line Numbers and Proper Code Alignment/Indentation
New Scala Worksheets Not Evaluated in Eclipse
Add Blank Line Between Lines from Different Groups
Why Didn't I Get Segmentation Fault When Storing Past the End of the Bss
Quick Unix Command to Display Specific Lines in the Middle of a File
How to Find Out Why My Storage Space on Amazon Ec2 Is Full
Does Os X Have an Equivalent to /Dev/Shm
What Does Anon-Rss and Total-Vm Mean
Bash: Let Statement VS Assignment
How to Export Dbus_Session_Bus_Address
Mount Remote Windows Share from Centos
Openvpn on Linux: Passing Username and Password in Command Line