Professional Documents
Culture Documents
1
IOS203_Ch4
Objectives
Upon completion of this chapter, the student should be able to:
Keywords
history, .bash_history, HISTSIZE, HISTFILE, HISTFILESIZE, vi, vim, Vim Modes.
2
IOS203_Ch4
The bash shell has many tools and facilities to help you type your commands quickly and correctly
without memorizing every command and argument. It stores a history of executed commands,
which can be used to repeat commands when needed. It has newer version of vi, the standard
Unix and easy text editor. Furthermore, the bash shell expands certain command line
metacharacters before interpreting the command.
The history command (alone without options or arguments) displays all the shell command
history. Typing history followed by a number causes only that number of the latest commands to
appear. For example, to show only the last 10 commands, run the command line:
You can also use the following command line to show the same result:
Typing history -c clears the history, which can be handy if you have recently typed commands
you’d rather not have discovered by others (such as commands that include passwords).
To pull the command out of the history, you should press the Up arrow key on your keyboard;
this brings up the previous command. Pressing the Up arrow key repeatedly moves through
multiple commands so you can find the one you want. If you miss the wanted command, press
the Down arrow key to move down the history. You can also use the Ctrl+P and Ctrl+N keystrokes
double for the Up and Down arrow keys, respectively.
3
IOS203_Ch4
HISTSIZE variable
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. You can show and
modify the value of HISTSIZE variable as follows:
In order to change permanently the value of HISTSIZE, you should modify the value in the
~/.bash_profile as follows:
HISTFILE variable
The HISTFILE variable points to the file that contains your history. In the bash shell, the default
value of HISTFILE variable is ~/.bash_history.
HISTFILESIZE variable
The HISTFILESIZE variable determines the number of commands kept in your history file.
The difference between HISTSIZE and HISTFILESIZE is that HISTSIZE limits the number of
commands shown by the command history while HISTFILESIZE limits the number of commands,
which can be saved in $HISTFILE.
When one exits the bash, if there are more than $HISTSIZE number of commands which have
been executed in the single bash session, the contents of $HISTFILE will be replaced by the
$HISTSIZE number of commands. If there are less than or equal to $HISTSIZE number of
commands in the bash session, these commands will be appended to $HISTFILE as long as
$HISTFILESIZE permits.
You can use ctrl-r to search in the history. In the example below I typed ctrl-r followed by two
characters hi and it finds the last command containing these two consecutive characters.
4
IOS203_Ch4
! Character
When you type ! followed by the number representing the command you want repeated, then
the shell will echo the command and execute it.
Some other advanced ways of retrieving commands from the history are shown in the following
table:
Trick Description
!! repeat last command
!char repeat last command that started with char
!-n repeat a command entered n commands back
5
IOS203_Ch4
If the file exist, the file is opened and the content is displayed. If the file doesn’t exist, vi creates
it when you save it for the first time.
Vim modes
Vim has three modes: Insertion mode, Command mode and Ex mode (or last line mode).
Command mode: this mode accepts commands to move cursor and delete/paste text.
Usually, these commands are entered as single character. For instance, (i) enters Vim in Insert
mode;
Insert mode: you use this mode to type new text. When you press Esc key, you returns Vim
to Command mode;
Ex mode: you use this mode to manipulate files (including save and exit), and to search and
replace function. You enter Vim in Ex mode from Command mode by typing a colon (:).
If you are not sure what mode Vim is in, press the Esc key to return to Command mode, from
which you can reenter Insert mode. The following table shows the most common text-editing
commands:
command Description
Quitting
ZZ save and quit
:x save and quit
:q quit (unless changes)
:q! quit (force, even if unsaved)
:w save without exit
:w! forcing save changes without exit
:wq Save and quit
:e! toggle with last edited file
Inserting Text (insert mode)
i,I insert before cursor, before line
a,A append after cursor, after line
o,O open new line after, line before
r,R replace one char, many chars
Cursor Movement
h,j,k,l left, down, up, right
w,W next word, blank delimited word
b,B beginning of word, of blank delimited word
6
IOS203_Ch4
7
IOS203_Ch4
Brace expansion { }
For example, the expression {c,b,p}ut would expand into the three words cut but put. You can
quickly create a set of directories (or files) using one command:
In the mkdir command, the braced word is expanded to the three directories Dir01, Dir02 and
Dir03.
Tilde expansion ~
The tilde expansion merely expands a ~ (or ~username) to the user's (or the username's) home
directory, as listed in the /etc/passwd file.
The bash shell will expands expressions of the form $VARNAME or ${VARNAME} to the value of
the shell or environment variable VARNAME.
The double quotes "..." protects enclosed characters from being interpreted by the shell, with the
exceptions of the $, !, and ` (backtick) characters. They are used when you would like to treat
most punctuation literally or to combine words into a single token, but still be able to make use
of variables, command substitution, and history substitution.
The single right quotes '...' protects all enclosed characters from being interpreted by the shell.
They are used in similar situations as double quotes when you want all punctuation, including
variables and command substitution, to be treated literally.
Single left quotes (Back Ticks) `...` are not used for quoting. Instead, back ticks are used to invoke
command substitution on the include text.
8
IOS203_Ch4
Arithmetic expansion
Arithmetic operators such as +, -, *, and / on the command line do not have their traditional
mathematical meaning. However, the bash shell treats them within a $((...)). First, variables are
treated as numeric integers where appropriate, and secondly, standard mathematical operators
such as +, -, *, and / are treated as such. The bash shell will "expand" the entire expression, and
replace it with the numeric result. Here are two examples:
Command substitution
Command substitution allows users to run arbitrary commands in a subshell and incorporate the
results into the command line. You can do that by using:
`subcommand`: “back ticks", the left leaning single quote, found on the same key as ~,
next to the 1 key on most keyboards;
$(subcommand): similar to arithmetic expansion, but with only one set of parentheses.
Tab completion
You can use <TAB> key to complete command line:
Try to type:
If you down’t remember the spelling of a command (or a script name), you can type first two
letters and then type <TAB> twice. For example, try to type:
9
IOS203_Ch4
and now, you can choose the command you want to use.
Trick Description
10
IOS203_Ch4
Questions
1. Type the command: ls –ld, then repeat the previous command using only two characters
(there are more than one solution)
2. Display the last 6 commands you typed
3. Issue the command you typed in question 1 again, using the line numbers you received from
the command in question 3.
4. How many commands can be remembered in memory for your current shell session?
5. When exiting the shell, where are these commands stored?
6. When exiting your current shell session, how many commands can be written to the history
file?
7. How can you verify that your current bash shell remembers the next 5000 commands you
type.
8. Open more than one console with the same user account. When is command history written
to the history file?
9. Using vi(m) editor to answer the following questions:
a. What 3 key sequence in command mode will duplicate the current line.
b. What 3 key sequence in command mode will switch two lines' place (line five
becomes line six and line six becomes line five).
c. What 2 key sequence in command mode will switch a character's place with the
next one.
10. Create a directory and enter it. Then create the following files inside the directory using one
command: file1 file10 file11 file2 File2 File3 file33 fileAB filea fileA fileAAA file( file 2
(the last one “file 2” has 6 characters including a space)
11. List all files starting with file or File
12. List all files starting with file and ending with a letter
13. List all files starting with File and having a digit as fifth character and nothing else.
14. List all files starting with a letter and ending in a number.
15. List all files that have exactly five characters.
16. List all files that start with f or F and end with 3 or A.
17. List all files that start with f have i or R as second character and end in a number.
18. List all files that do not start with the letter F.
19. Can echo command replace ls command? How can you list the files in the current directory
with echo?
20. Is there another command besides cd to change directories?
References
[1] Red Hat Linux Essentials RH033-RHEL5-en-2-20070306
[2] Paul Cobbaut, “Linux Fundamentals”, https://linux-training.be/funhtml/index.html. Updated
on 2015-05-24
11