Bash – View a range of bash history

bashcommand history

The history command lists out all the history for the current session. Like:

1 ls 
2 cd /root
3 mkdir something
4 cd something
5 touch afile
6 ls
7 cd ..
8 rm something/afile
9 cd ..
10 ls
11 history

In order to search items of interest, I can pipe history with grep like

history | grep ls
1 ls
6 ls
10 ls

I can also view last 3 commands like:

history 3
11 history
12 history | grep ls
13 history 3

But how do I get a specific range of history? For example something like:

history range 4 7
4 cd something
5 touch afile
6 ls
7 cd ..

Best Answer

Instead of history, you can use fc, which allow you select range:

fc -l 4 7
Related Question