Chapter 9. Controlling Processes

Shell scripts were designed to run commands. Up to this point, all the scripts in the book have launched various commands, but all in isolation. The most you've seen so far is piping commands to connect the output of one command to the input of another. But the commands run from the scripts do not provide data back to the scripts, other than through writing data to a file. To make processes better fit into shell scripts, you need the capability to start and stop processes, as well as capture the output of processes into shell variables.

This chapter delves into processes and shows how you can launch and control processes from your scripts, including:

  • Exploring the processes running on your system

  • Launching processes in the foreground and the background

  • Using command substitution to set variables from commands

  • Checking the return codes of processes

Exploring Processes

There is a lot of terminology associated with processes. Don't be daunted; it's easier to understand when you see it in action than to explain.

Simply put, a process is a running program. A program is a file on disk (or other storage) that can be executed. Most programs are compiled into the binary format required by the processor chip and operating system. For example, the ls command is a program, compiled for a particular system. An ls program compiled for Linux on a Pentium system will not run in Windows, even on the same computer and processor chip. That's because the operating system defines ...

Get Beginning Shell Scripting now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.