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.
Table of Contents
Display Bash History Without Line Numbers
When you run the history
command, you will see an output something like below.
$ history
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:
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:
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:
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 characters8-
: (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*//'
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:
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.
Suggested Read:
- How To Clear Command Line History In Linux
- How To Clear A Specific Command From Bash History In Linux
- HSTR – Easily View, Navigate, Search And Manage Your Commandline History
- McFly – A Replacement To ‘Ctrl+R’ Bash History Search Feature
- Bashhub – Access Your Terminal History From Anywhere
- How To Find Top Most Used Commands On Linux