Friday, May 2, 2025
Home How to How to view Bash history without line numbers?

How to view Bash history without line numbers?

by blackMORE
8 comments
How to view bash history command without line numbers - Bash history wo line numbers - blackMORE Ops - 2

bash history command is very useful. It gives you an complete view of what commands you ran. By default bash  historycommand will give you all your previous commands with Line numbers. It’s the default behavior. However when you’re trying to copy-paste those commands again, you have to manually remove those Line numbers. This becomes rather annoying when you are trying to copy paste a lot of commands at the same time. This is very simple guide on how to view bash history without line numbers.

bash history with line numbers

When you type in history in your bash terminal, following what you see.

root@kali:~# history 
-------snip------------
 2002  clear
 2003  ls
 2004  cd
 2005  top
 2006  nethogs wlan0
 2007  htop
 2008  sar -r
 2009  free -m
 2010  pstree
 2011  pgrep gdm3
 2012  w
 2013  who
 2014  last | head
-------snip------------

This is very normal behavior.

How to view Bash history without line numbers - Normal outout - blackMORE Ops - 1

My target is to remove these line numbers and just view the actual commands so that I can copy paste them easily.

View bash history without line numbers

Extremely simple command. We utilize cut to do this job:

root@kali:~# history | cut -c 8-

cut =  cut removes sections from each line of files

-c   = -c will select only certain characters

8-  = 8-  or N- will show output from N’th byte, character or field, to end of line. In this case, bash history command shows actual command from 8th character. Adjust your command to match your output.

Output for bash history command without line numbers

-------snip------------
clear
history 
history | tail
clear
history 
clear
last | head
w
whoami
uname -a
lsb_release -a
clear
history | tail
history | cut -c 8-
-------snip------------

How to view bash history command without line numbers - Bash history wo line numbers - blackMORE Ops - 2

Hope this helps someone out there.

Credit: http://stackoverflow.com/questions/7110119/bash-history-without-line-numbers

You may also like

8 comments

Carlos Ortigoza Dempster October 21, 2014 - 2:20 pm

Flawless

Reply
Bavarian October 21, 2014 - 5:15 pm

Thank you.

Reply
GoNzO October 22, 2014 - 12:05 am

or you just type “cat ~/.bash_history”

Reply
blackMORE Ops October 22, 2014 - 12:15 am

You could. But default bash config (aka .bash_history) doesn’t log correctly when you got multiple terminals open.

Reply
GoNzO October 23, 2014 - 7:15 pm

Ok, i missed that… Thanks you!

Reply
xan December 16, 2014 - 7:40 pm

Additionally, if you just want to execute a command again, you can use the line numbers!

For instance (your example):

> 2006 nethogs wlan0

We could run:

!!2006 [enter] # and bash will translate this to ‘nethogs wlan0’

Reply
blackMORE Ops December 16, 2014 - 7:45 pm

You’re right.
But it would fail if there’s no bash. (i.e sh only or ksh or other shell). Wouldn’t it?

(I’m traveling now, will check when I get back home.)

Reply
Marthand September 4, 2015 - 10:21 pm

history -w my_history.txt

Reply

Leave a Reply to blackMORE OpsCancel reply

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

About Us

Lorem ipsum dolor sit amet, consect etur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis..

Feature Posts

Newsletter