c) History, Command Line Editing and Job Control

History

The history command lists the last commands you typed.

Command history
-bash-4.1$ history
    1  cd
    2  ls -l
    3  who 
    4  ps
    5  sleep 200 &
    6  ps
    7  fg
    8  history
-bash-4.1$ 

Command Line Editing

You can select past commands using the up and down arrow keys. You can edit the command line using the left and right arrow keys and any of the following commands:

keystrokeresult
Ctrl-P

previous command

Ctrl-Nnext command
Ctrl-R stringprevious command containing string
Ctrl-Bmove back one character
Ctrl-Fmove forward one character
DELdelete previous character
Ctrl-Ddelete character under cursor
ESC-Ddelete word forward
ESC-Hdelete word backward
Ctrl-Ttranspose two characters
ESC-F

move forward one word

ESC-Bmove back on word

Job Control

Job control deals with managing your programs whilst they are running. Linux uses the name process for a running program. The ps command list all the processes you have running.

Listing your running processes
-bash-4.1$ ps
  PID TTY          TIME CMD
14521 pts/3    00:00:00 bash
16165 pts/3    00:00:00 ps
-bash-4.1$ 

From this we can see that we have two processes running, the bash shell and the ps command. Associated with each process is a unique identifier - PID (process ID).

We can manage processes, especially command that take a long time to run, by making them in the background process adding an '&' to the end of the line. The shell then becomes free for us to execute more commands.

Management of background processes
-bash-4.1$ ps
  PID TTY          TIME CMD
14521 pts/3    00:00:00 bash
17005 pts/3    00:00:00 ps
-bash-4.1$ sleep 120 
^C
-bash-4.1$ sleep 120 &
[1] 17026
-bash-4.1$ ps
  PID TTY          TIME CMD
14521 pts/3    00:00:00 bash
17026 pts/3    00:00:00 sleep
17027 pts/3    00:00:00 ps
-bash-4.1$ echo I am doing other work
I am doing other work
-bash-4.1$ echo my work is complete
my work is complete
[1]+  Done                    sleep 120
-bash-4.1$ 

The sleep command does nothing for the number of seconds specified in the argument. The first invocation of sleep is terminated (killed) by the impatient user typing CTRL-C. The second invocation places the command in that background, we are then able to do other work. The Done statement informs us that the command has terminated.

Managing your background jobs
-bash-4.1$ sleep 360 &
[1] 17761
-bash-4.1$ sleep 1000 &
[2] 17766
-bash-4.1$ jobs
[1]-  Running                 sleep 360 &
[2]+  Running                 sleep 1000 &
-bash-4.1$ fg
sleep 1000
^Z
[2]+  Stopped                 sleep 1000
-bash-4.1$ jobs
[1]-  Running                 sleep 360 &
[2]+  Stopped                 sleep 1000
-bash-4.1$ bg
[2]+ sleep 1000 &
-bash-4.1$ jobs
[1]-  Running                 sleep 360 &
[2]+  Running                 sleep 1000 &
x-bash-4.1$ fg %1
sleep 360
^C
-bash-4.1$ jobs
[2]+  Running                 sleep 1000 &
-bash-4.1$ 

In this example we put two jobs into the background. The fg command moves the last job placed in the background into the foreground. CTRL-Z stops (pauses, not kills) the job and returns to the command line. the bg command places the paused job in the background. fg can bring specific jobs to the foreground by specifying the job number.

Â