Bash history without line numbers

LinuxBash

Linux Problem Overview


The bash history command is very cool. I understand why it shows the line numbers, but is there a way I can invoke the history command and suppress the line numbers?

The point here is to use the history command, so please don't reply cat ~/.bash_history

Current Output:

  529 man history
530 ls
531 ll
532 clear
533 cd ~
534 history
Historical graphic source.

Desired Output:

man history
ls
ll
clear
cd ~
history
Historical graphic source.

Thanks to everyone for your great solutions. Paul's is the simplest and will work for me for because my bash history size is set at 2000.

I also wanted to share a cool article I found this morning. It has a couple good options that I am now using, like keeping duplicate entries out of the bash history and making sure multiple bash sessions don't overwrite the history file: http://blog.macromates.com/2008/working-with-history-in-bash/

Linux Solutions


Solution 1 - Linux

Try this:

$ history | cut -c 8-

Solution 2 - Linux

awk can help:

history|awk '{$1="";print substr($0,2)}'

This answer can fail if you have a long history.

Solution 3 - Linux

If you were willing to switch to zsh isntead of bash, then zsh supports this natively (as well as other options for history formatting)

zsh> fc -ln 0

(See https://serverfault.com/questions/114988/removing-history-or-line-numbers-from-zsh-history-file)

Solution 4 - Linux

history -w /dev/stdout

From output of history --help:

> -w write the current history to the history file

It writes current history to specified file - /dev/stdout in this case.

Solution 5 - Linux

I'm late on this one, but the shorter method would be to add the following in your ~/.bashrc or ~/.profile file:

From bash manpage:

       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.  This uses
the history comment character to distinguish timestamps from
other history lines.

Using this capability, a smart hack consist in making the variable "print" a carriage return (\r) and clear the line (ANSI code K) instead of an actual timestamp.

Solution 6 - Linux

Alternatively, you could use sed:

history | sed 's/^[ ]*[0-9]\+[ ]*//'

Using alias, you can set this as your standard (stick it in your bash_profile):

alias history="history | sed 's/^[ ]*[0-9]\+[ ]*//'"

Solution 7 - Linux

Although cut with the -c option works for most practical purposes, I think that piping history to awk would be a better solution. For example:

history | awk '{ $1=""; print }'

OR

history | awk '{ $1=""; print $0 }'

Both of these solutions do the same thing. The output of history is being fed to awk. Awk then blanks out the first column, which corresponds to the numbers in the history command's output. Here awk is more convenient because you don't have to concern yourself with the number of characters in the number part of the output.

print $0 is equivalent to print, since the default is to print everything that appears on the line. Typing print $0 is more explicit, but which one you choose is up to you. The behavior of print $0 and simply print when used with awk is more evident if you used awk to print a file (cat would be faster to type instead of awk, but this is for illustrating a point).

[Ex] Using awk to display the contents of a file with $0

$ awk '{print $0}' /tmp/hello-world.txt
Hello World!

[Ex] Using awk to display the contents of a file without explicit $0

$ awk '{print}' /tmp/hello-world.txt
Hello World!

[Ex] Using awk when the history line spans multiple lines

$ history
   11  clear
   12  echo "In word processing and desktop publishing, a hard return or paragraph break indicates a new paragraph, to be distinguished from the soft return at the end of a line internal to a paragraph. This distinction allows word wrap to automatically re-flow text as it is edited, without losing paragraph breaks. The software may apply vertical whitespace or indenting at paragraph breaks, depending on the selected style."

$ history | awk ' $1=""; {print}'
 clear
 echo "In word processing and desktop publishing, a hard return or paragraph break indicates a new paragraph, to be distinguished from the soft return at the end of a line internal to a paragraph. This distinction allows word wrap to automatically re-flow text as it is edited, without losing paragraph breaks. The software may apply vertical whitespace or indenting at paragraph breaks, depending on the selected style."

Solution 8 - Linux

history command does not have an option to suppress line numbers. You will have to combine multiple commands as everyone is suggesting:

Example :

history | cut -d' ' -f4- | sed 's/^ \(.*$\)/\1/g'

Solution 9 - Linux

$ hh -n

You may want to try https://github.com/dvorka/hstr which allows for "suggest box style" filtering of Bash history with (optional) metrics based ordering i.e. it is much more efficient and faster in both forward and backward directions:

enter image description here

It can be easily bound to Ctrl-r and/or Ctrl-s

Solution 10 - Linux

You can use command cut to solve it:

Cut out fields from STDIN or files.

  • Cut out the first sixteen characters of each line of STDIN: cut -c 1-16

  • Cut out the first sixteen characters of each line of the given files: cut -c 1-16 file

  • Cut out everything from the 3rd character to the end of each line: cut -c3-

  • Cut out the fifth field of each line, using a colon as a field delimiter (default delimiter is tab): cut -d':' -f5

  • Cut out the 2nd and 10th fields of each line, using a semicolon as a delimiter: cut -d';' -f2,10

  • Cut out the fields 3 through 7 of each line, using a space as a delimiter: cut -d' ' -f3-7

Solution 11 - Linux

I know I am late for the party but this is just so much easier to remember:

cat ~/.bash_history

Solution 12 - Linux

If you are trying to send your history without line numbers to a file and want to have the file for later reference please read below:

history | sed 's/^[ ]*[0-9]\+[ ]*//' >>history.txt

The above command will read your history's content into a text file called history. Which will allow you to have different versions as you progress through your project(s).

I like it, because it helps me simplify automation when executing a bash script (wink)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncwdView Question on Stackoverflow
Solution 1 - LinuxPaul RView Answer on Stackoverflow
Solution 2 - LinuxZsolt BotykaiView Answer on Stackoverflow
Solution 3 - LinuxAtt RighView Answer on Stackoverflow
Solution 4 - LinuxczernyView Answer on Stackoverflow
Solution 5 - LinuxiMilView Answer on Stackoverflow
Solution 6 - LinuxManny DView Answer on Stackoverflow
Solution 7 - LinuxSuave-VView Answer on Stackoverflow
Solution 8 - Linuxring bearerView Answer on Stackoverflow
Solution 9 - LinuxMartin DvorakView Answer on Stackoverflow
Solution 10 - LinuxGEOKINGView Answer on Stackoverflow
Solution 11 - LinuxMike SlinnView Answer on Stackoverflow
Solution 12 - LinuxDon-Pierre HalfawayView Answer on Stackoverflow