Linux Shell history
In this session, we have covered repeat commands in the shell, history etc.
repeating the last command
To repeat the last command in bash, type !!. This is pronounced as bang bang.
datasoft @ datasoft-linux ~$ cd test2
datasoft @ datasoft-linux ~/test2$ echo this will be repeated > test2.txt
datasoft @ datasoft-linux ~/test2$ !!
echo this will be repeated > test2.txt
datasoft @ datasoft-linux ~/test2$
repeating other commands
You can repeat other commands using one bang followed by one or more characters. The shell will repeat the last command that started with those characters.
datasoft @ datasoft-linux ~/test2$ touch test2
datasoft @ datasoft-linux ~/test2$ cat test2
datasoft @ datasoft-linux ~/test2$ !to
touch test2
datasoft @ datasoft-linux ~/test2$
history
To see older commands, use history to display the shell command history (or use history n to see the last n commands).
datasoft @ datasoft-linux ~/test2$ history 10
1185 set +C ; set +u
1186 echo $-
1187 clear
1188 cd test2
1189 echo this will be repeated > test2.txt
1190 touch test2
1191 cat test2
1192 touch test2
1193 clear
1194 history 10
datasoft @ datasoft-linux ~/test2$
quotes
Notice that double quotes still allow the parsing of variables, whereas single quotes prevent this.
The bash shell will replace variables with their value in double quoted lines, but not in single quoted lines.
!n
When typing ! followed by the number preceding the command you want to be repeated, then the shell will echo the command and execute it.
datasoft @ datasoft-linux ~/test2$ !1189
echo this will be repeated > test2.txt
datasoft @ datasoft-linux ~/test2$ ls
test2 test2.txt
datasoft @ datasoft-linux ~/test2$
Ctrl-r
Another option is to use ctrl-r to search in history. In the screenshot below i only typed ctrl-r followed by four characters apti and it finds the last command containing these four consecutive characters.
datasoft @ datasoft-linux ~/test2$
(reverse-i-search)`i': sudo vim apti install screen
$HISTSIZE
The $HISTSIZE variable determines the number of commands that will be remembered in your current environment. Most distributions default this variable to 500 or 1000.
datasoft @ datasoft-linux ~/test2$ echo $HISTSIZE
1000
You can change it to any value you like.
datasoft @ datasoft-linux ~/test2$ HISTSIZE=20000
datasoft @ datasoft-linux ~/test2$ echo $HISTSIZE
20000
$HISTFILE
The $HISTFILE variable points to the file that contains your history. The bash shell defaults this value to ~/.bash_history.
datasoft @ datasoft-linux ~/test2$ echo $HISTFILE
/home/datasoft/.bash_history
A session history is saved to this file when you exit the session!
Closing a gnome-terminal with the mouse, or typing reboot as root will NOT save your terminal's history.
$HISTFILESIZE
The number of commands kept in your history file can be set using $HISTFILESIZE.
datasoft @ datasoft-linux ~/test2$ echo $HISTFILESIZE
2000
(optional)regular expressions
It is possible to use regular expressions when using the bang to repeat commands. The screenshot below switches 1 into 2.
datasoft @ datasoft-linux ~$ cat part1
one
datasoft @ datasoft-linux ~$ !c:s/1/2
cat part2
two
datasoft @ datasoft-linux ~$
(optional)repeating commands in ksh
Repeating a command in the Korn shell is very similar. The Korn shell also has the history command, but uses the letter r to recall lines from history.
This screenshot shows the history command. Note the different meaning of the parameter.
1271 cd ..
1272 ls
1273 clear
1274 cat part1
1275 cat part2
1276 r
1277 $ history 17
1278 sudo apt-get r
1279 clear
1280 history 10
Repeating with r can be combined with the line numbers given by the history command, or with the first few letters of the command.
Exercise, Practice and Solution:
1. Issue the command echo The answer to the meaning of life, the universe and everything is 42.
Code:
echo The answer to the meaning of life, the universe and everything is 42);
2. Repeat the previous command using only two characters (there are two solutions!)
Code:
!!
OR
!e
3. Display the last 5 commands you typed.
Code:
datasoft @ datasoft-linux ~$ history 5
52 ls -l
53 ls
54 df -h | grep sda
55 echo The answer to the meaning of life, the universe and everything is 42
56 history 5
You will receive different line numbers.
4. Issue the long echo from question 1 again, using the line numbers you received from the command in question 3
Code:
datasoft @ datasoft-linux ~$ !56
echo The answer to the meaning of life, the universe and everything is 82
The answer to the meaning of life, the universe and everything is 82
5. How many commands can be kept in memory for your current shell session?
Code:
echo $HISTSIZE
6. Where are these commands stored when exiting the shell ?
Code:
echo $HISTFILE
7. How many commands can be written to the history file when exiting your current shell session?
Code:
echo $HISTFILESIZE
8. Make sure your current bash shell remembers the next 5000 commands you type.
HISTSIZE=5000
9. Open more than one console (press Ctrl-shift-t in gnome-terminal) with the same user account. When is command history written to the history file?
when you type exit
Previous:
Linux shell variables
Next:
Linux file globbing
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/linux-system-administration/shell-history.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics