CHAPTER 10 · 6 lessons · 78 exercises

The shell environment

What happens to what you type before it ever reaches the command.

Up to now you have thought that what you type goes to the command as it is. It does not, in fact.

Between you and Linux there is an interpreter called the shell, and it rewrites your letters the moment you press ⏎. Spreading *.txt into a list of file names, putting the real place in for $HOME — that is the shell's work, not the command's.

  • environment variables — echo $HOME export env
  • PATHthe very mechanism by which a command is found
  • quoting and escaping — the difference between ' and " and \
  • the order of expansion — when * ? ~ $ widen out
  • aliases and history — alias history
  • .profile — the settings read every time you start

This is the chapter with the most "ah, I see" in it. Why typing ls runs ls (PATH), why a space in a file name breaks things (expansion) and why you choose between the quotes (whether $ widens out or not) all join up and come into view.

Once you have this, the way you read an error message changes too, because you can tell apart "the command is at fault" from "the shell handed it something else".

Lessons in this chapter

Variables and PATH

  1. 52Containers for values (variables)Make the shell remember things. Passing them to a child needs export.Go to the exercises
  2. 53How a command gets found (`PATH`)Why typing ls runs ls. Building a toolbox of your own.Go to the exercises

Quoting and expansion

  1. 54Guarding with quotes (`'`, `"` and `\`)Let the shell rewrite it, or hand it over as it is. Handling names with spaces in them.Go to the exercises
  2. 55The order things expand in (`~`, `$`, `*`)What the shell does, and in what order, between the return key and the command running.Go to the exercises

Make it a tool of your own

  1. 56Aliases and historyGive a long command a short name, and call back the moves you typed.Go to the exercises
  2. 57Writing it down in `.profile`Write your own settings in the file that is read every time you open a terminal.Go to the exercises

Next up is “Write a script”. Put the steps you typed into a file, so you can call them by name.