So, how did we arrive at this and will this always work? No it might not always work. A typical example is where HISTTIMEFORMAT variable is set. In that case, if you check history, you will see that after the number column we have time and date in the specified format, in which case, you will get wrong information from the above command. Anyways, forgetting these special cases, lets go to how we got this command:

1

history|awk '{print $2}'

will give us list of all the commands that we have used and are in history. But this will also give commands like “history|more” as one command as this does not have any space. So, we eliminate whatever is there after the “|” with awk command

1

history|awk '{print $2}'|awk -F"|" '{print $1}'

or

1

history | awk '{print $2}' | awk 'BEGIN {FS="|"}{print $1}'

and now to get all the counts, we need to sort and then count unique occurances: