Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Example: shells in shells

On your Vagrant VM, you’ll be executing a short series of commands in order to better understand the correct behavior. We’ll primarily be making use of two commands, ps and jobs. Recall that ps gives you information about all processes running on the system, while jobs gives you a list of jobs that the current shell is managing. Enter the following commands in your terminal, and you should see similar behavior:

$ ps
PID TTY TIME CMD
20970 ttys002 0:01.30 -bash
$ sh
sh-3.2$ ps
PID TTY TIME CMD
20970 ttys002 0:00.63 -bash
22323 ttys004 0:00.01 sh

At this point, we have started a sh shell within our bash shell.

sh-3.2$ cat
hello
hello
world
world
^Z
[1]+ Stopped(SIGTSTP) cat
sh-3.2$ ps
PID TTY TIME CMD
20970 ttys004 0:00.63 -bash
22323 ttys004 0:00.02 sh
22328 ttys004 0:00.01 cat

Notice how sending a CTRL-Z while the cat program was running did not suspend the sh nor the bash programs.

sh-3.2$ jobs
[1]+ Stopped(SIGTSTP) cat
sh-3.2$ exit
$ ps
PID TTY TIME CMD
20970 ttys004 0:00.65 -bash

Since exit terminates the shell, we terminated the sh program. Enter exit again and your terminal will close.

Before we explain how you can achieve this effect, let’s discuss some more operating system concepts.