Home Linux Tips & Tricks How To Display Bash History Without Line Numbers

How To Display Bash History Without Line Numbers

By sk
Published: Last Updated on 24.1K views

As you know already, the Bash history file (~/.bash_history) keeps a record of all the commands you run in the Terminal. We can run the history command to view the list of recently-executed commands in our Terminal. When you look into the history, you should have noticed that each command has a prefix number (line number). This prefix number helps you to find and delete a specific command entry from the history. But, did you know we can omit those numbers from the history command's output? This brief tutorial describes all the possible ways to display Bash history without line numbers on Linux and Unix-like operating systems.

Display Bash History Without Line Numbers

When you run the history command, you will see an output something like below.

$ history
Bash history output
history command output

As you can see, each entry in history comes with a prefix number.

If you don't want to display the line numbers in history command's output, here are a few ways to do it.

Method 1 - Display contents of ~/.bash_history file

This is the simplest way to display Bash history without line numbers.

$ cat ~/.bash_history

Sample output:

Display Bash History Without Line Numbers
Display Bash History Without Line Numbers

Method 2 - Using history command

We can use the history command's write option to print the history without numbers like below.

$ history -w /dev/stdout

Sample output:

Bash Shell History Without Line Numbers Using history command
Bash Shell History Without Line Numbers Using history command

If you want to write the output to a text file, say history.txt, simply run:

$ history -w history.txt

Method 3 - Using history and cut commands

One such way is to use history and cut commands like below.

$ history | cut -c 8-

Sample output:

Bash Shell History Without Line Numbers
Bash Shell History Without Line Numbers

See? There are no line numbers.

Let us break down the above command and see what each part does.

  • history : Displays your Shell history.
  • | : Pipe symbol is used separate one or more commands. It sends output of one command as input of the next command. That is, each command reads the previous command’s output.
  • cut : Remove sections from each line of files
  • -c : Select only certain characters
  • 8- : (N-) Display output from N'th byte, character or field, to end of line. In this case (8-), it cuts the first 7 characters of each line of output of the history command and shows the actual command from 8th character.

Method 4 - Using fc and sed commands

We can also use fc command and sed commands to display shell history without line numbers.

$ fc -l -n 1 | sed 's/^\s*//'
Display Shell History Without Line Numbers using fc command
Display Shell History Without Line Numbers using fc command

To know what each part does in the above Linux command, refer ExplainShell.

Method 5 - Using history and awk commands

Another way to ignore line numbers shell history is to use "awk" command like below.

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

Sample output:

Display Shell History Without Line Numbers using awk command
Display Shell History Without Line Numbers using awk command

Method 6 - Using Perl

The yet another way to display Bash history without line numbers is by running the following Perl one-liner command:

$ history | perl -pe "~s/ *[0-9]+ *//"

I don't know exact use case for this. Just in case if you're ever in a situation where to display your Shell history without the prefix numbers, these methods will help.

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More