Most Shells Provide Powerful mechanism for
handling History that helps you to recall commands or repeat them. This is very
helpful when executing a long command.
The History command available in the Bash
shell shows you the available commands executed until now much like,
[root@vx111a ~]# history
989 cd domains/
990 ll
991 clear
992 cd foo_domain/
994 cat fileRealm.properties
999 ./startWebLogic.sh
1000
exit
1001
history 10
*
*
We can get a specific number of commands using,
history 20 (which gives you last 20 commands in the history).The commands that
we normally execute are saved in the file /root/.bash_history.
History
Env variables
HISTFILE: There is an env variable called ‘HISTFILE’
which is pointed to the /root/.bash_history file making it to save commands
executed. If we unset this env variable, the commands are not saved when shell
exited.we can change this by
Export HISTFILE=/root/.commandline_warrior
HISTSIZE: This is another env variable
specifying the number of commands to be remembered by the command history. The
default size is 500. Just export the variable as
export HISTSIZE= 3000
HISTTIMEFORMAT: This env variable enabled
to add time stamp to the associated command executed. Just export the variable
as
export HISTTIMEFORMAT = ‘%F %T ‘
HISTCONTROL : When working at the command
line we often end up executing some commands multiple times. The default
history size is 500, too many duplicates of the same commands can fill up your
history and leave you with a less then useful history. We can use this env
variable to make that duplicate commands are not saved in history. This can
done by adding
export HISTCONTROL= ignoredups
But the above env remove duplicate
command if they are consecutive. If we need to erase the duplicated completely all
over the history add,
export
HISTCONTROL=erasedups
We can also force history not to remember particular
commands .This is done by adding
export
HISTCONTROL=ignorespace
At this point, when you enter a command
with a ‘space’ before it. The command gets executed but it is not saved in
history file.
If we need to make history ignore specific commands,
we can
export
HISTIGNORE="pwd:ls:ls -ltr:"
This will make sure that the commands are
not allowed to execute.
Clean
a History
If we need to clean the history completely,
we can use ‘history –c’
Write
& Read
We
can make sure that our history can be written to a specific file ( besides
/root/.bash_history). This can done using
history -w hist.txt
And Reading history from a file is also
possible by
history -r hist.txt
Here is a list of examples that can be
working on the bash shell along with history command
!!
|
Execute the Previous Command
|
!n
|
Execute the Nth command
!1000 : execute the command number 1000
which is saved in the history file
|
!$
|
Take the arguments from the previous
command
[root@vx111a
test]# cat sam1
THIS IS JAGADISH1
[root@vx111a
test]# nl !$
nl sam1
1 THIS IS JAGADISH1 |
!:0
|
Take the Previous Command
[root@vx111a
test]# cat sam1
THIS IS JAGADISH1 [root@vx111a test]# !:0 sam2 cat sam2 THIS IS PAVAN |
!^ ( Or ) ^
|
Take the First argument
[root@vx111a test]# cat sam1
THIS IS JAGADISH1 [root@vx111a test]# nl !^ nl sam1 1 THIS IS JAGADISH1 |
!:n
|
Take the arguments from the last command.
!:1 take the first argument , !:2 second and so on
[root@vx111a test]# cat sam1
THIS IS JAGADISH1 [root@vx111a test]# nl !:1 nl sam1 1 THIS IS JAGADISH1 [root@vx111a test]# mv sam1 tam1 [root@vx111a test]# cat !:2 cat tam1 THIS IS JAGADISH1 |
!:-
|
Previous Command
[root@vx111a test]# cat sam1
THIS IS JAGADISH1
[root@vx111a test]# !:- sam1 cat sam1 THIS IS JAGADISH1 |
!$
|
Takes the last Argument
[root@vx111a test]# mv tam1 sam1
[root@vx111a test]# nl !$ nl sam1 1 THIS IS JAGADISH1 |
!*
|
Take the Previous command argument
[root@vx111a test]# nl sam1
1 THIS IS JAGADISH1 [root@vx111a test]# cat !* cat sam1 THIS IS JAGADISH1 |
!!:g
|
Replace the Occurrences (replaces
all 1 with 2)
cat file1 file2 file3
!!:gs/1/2 ( when you execute this ,
all 1 will be replaced to 2 ) ,so the command would be cat file2 file2 file3
|
Happy Learning J