Chapter 3
Variables
Without variables it is difficult to get much done: You can’t count, loop, or read input from the user or the environment, and you can’t change anything much. Without variables, you cannot write much more than a basic batch script (do one thing, then do another, then do something else). With variables, you can modify the script’s behavior depending on the state of those variables, as well as modify the variables themselves to reflect the world beyond the script itself.
This chapter introduces the use of variables in the shell, and the syntax for setting and reading variables. It also lists some of the most common preset and standard shell variables and some useful shell options. Although a few of these variables are specific to bash, most of this chapter is generic across all shells. Chapter 7 goes into more depth on the more advanced things that bash (and some other shells) can do, such as arrays and more powerful parameter expansion features.