143
votes

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/

3
  • 4
    May I ask why cat ~/.bash_history is ruled out?
    – flow2k
    Commented Sep 3, 2019 at 21:19
  • 2
    @flow2k because that is saved history, (from last shell exit), NOT current history!
    – anthony
    Commented Oct 7, 2020 at 7:27
  • 1
    @anthony you can run history -a first to save your current history Commented Jan 7, 2021 at 15:23

12 Answers 12

226
votes

Try this:

$ history | cut -c 8-
10
  • Can we pipe in the output from the history command instead of reading the file?
    – cwd
    Commented Aug 18, 2011 at 15:45
  • 31
    man cut. It's deleting the first 7 characters of each line of output of the history command. It should only have problems if the number exceeds 99,999, something I've never seen (and I use shells a lot). But if you're concerned about that: history | sed 's/^ *[0-9]* *//' Commented Aug 18, 2011 at 15:54
  • 1
    I think @Paul R's solution is what I need. I didn't realize at first that the history command was padding the line numbers with spaces and now the cut syntax makes more sense :) Thanks @Keith Thompson for your solution that will work for > 100k histories.
    – cwd
    Commented Aug 18, 2011 at 16:00
  • 5
    @cwd: If you have 100,000 commands in your history, it's time to back away from the keyboard and go home. If you're already home, go outside. 8-)} (Yes, I know history can be retained across sessions.) Commented Aug 18, 2011 at 16:03
  • 1
    Well, HISTSIZE defaults to 500 or 1000 max, if you didn't change it cut -c 8- will never fail
    – nnsense
    Commented Aug 8, 2021 at 9:26
25
votes

awk can help:

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

This answer can fail if you have a long history.

1
  • Ha Ha, thanks - substr is so much simpler, I have been using history | awk '{for (i=2;i<=NF;i++) printf("%s ", $i);print("\r")}' for mine!! Commented Nov 6, 2014 at 14:00
19
votes

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)

2
  • 8
    Actually fc is a bash builtin as well. The only difference is that the first line is 1, so it would be fc -ln 1
    – wisbucky
    Commented Sep 15, 2017 at 23:32
  • while other commands work, and while this command isn't using history (which the OP specifically asked for), I'm upvoting this answer for its simplicity. Trying to remember the syntax for cut can be clunky for me, but this is as easy for me to remember as ls -hal Commented Sep 2, 2021 at 12:41
18
votes
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.

2
  • 3
    This is actually the simplest one to remember in practise! Clean and simple - plus no knowledge of regex/sed/awk needed off the top of your head :-) Commented Sep 12, 2020 at 22:24
  • This isn't working if you are not root, and you switched to that user with su because root will keep ownership of /dev/stdout, see this very good answer for more detail: unix.stackexchange.com/questions/38538/…. With a user, history -w /dev/tty works, but it's not exactly the same as stdout.
    – nnsense
    Commented Aug 8, 2021 at 10:15
11
votes

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

HISTTIMEFORMAT="$(echo -e '\r\e[K')"

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.

2
  • A bit simpler, using more obscure syntax: HISTTIMEFORMAT=$'\r\e[K'
    – wjandrea
    Commented May 20, 2017 at 21:12
  • 7
    And a one-line option: HISTTIMEFORMAT=$'\r\e[K' history
    – wjandrea
    Commented May 20, 2017 at 21:12
5
votes

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]\+[ ]*//'"
1
  • 1
    \+ in a basic regular expression is not POSIX conformant. Use \{1,\} if your sed doesn't support the non-standard \+ extension. Commented Apr 17, 2014 at 21:13
5
votes

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."
3
votes

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'
1
  • 3
    If you're going to run it through sed anyway, then the initial cut is redundant - just add it to the expression instead.
    – moopet
    Commented Jan 17, 2018 at 10:08
3
votes
$ 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

1
vote

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

0
0
votes

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

cat ~/.bash_history
3
  • 2
    True, but the second line of OP: "The point here is to use the history command, so please don't reply cat ~/.bash_history"
    – Cireo
    Commented May 2, 2020 at 1:43
  • That's great. As a bit lower level alternative which i've used.
    – kvdm.dev
    Commented May 26, 2021 at 9:11
  • 1
    Also, cat ~/.bash_history will just print out the content of that file. What about the commands still in memory? If you want this to really contain everything, you need to history -w before.
    – nnsense
    Commented Aug 8, 2021 at 10:19
0
votes

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)

Not the answer you're looking for? Browse other questions tagged or ask your own question.