Redirection, Expansion, and History
  1. Character Plumbing, Ch. 6
    1. Standard streams
      1. Three streams: Standard input, standard output, standard error.
      2. Programs read from standard input, write to standard output, and write errors to standard error.
      3. By default, input reads the keyboard and both standard output and standard error write the console. But any of those can be changed.
      4. Having standard output and error as separate streams allow regular output and error messages to be directed to different places.
    2. Notation for changing the connections.
      1. Use > to send stdout to a file.
      2. Use >> to append to the file.
      3. Use 2> to send stderr to a file.
      4. Use > file 2>&1 to send both to a file.
        1. The first one sends standard output to a file.
        2. The second sends standard error to standard output.
      5. The bit bucket: /dev/null
      6. Use < to end a file to standard input.
      7. Use | to connect standard out one program to standard input of another.
    3. Some commands whose streams can be usefully changed. All of these will read files if listed on the command line, but will read standard input otherwise.
      1. cat Copy input to output. May have multiple inputs which are concatenated.
      2. sort Sort input to output.
      3. uniq Eliminate duplicates in sorted input.
      4. wc Count words, lines and characters.
      5. grep Pass only matching input.
      6. head First part of a file.
      7. tail Last part of a file.
      8. tee Send input to both a file and standard out.
  2. Expansion.
    1. The shell reads your command line, breaks it into words, and descards the separating spaces.
    2. Some words aren't retained literally, but replaced with something else.
      1. This replacing is called “expansion.”
      2. Wildcards, already seen, are one example.
      3. As with wildcards, many expansions can replace one word with several.
    3. Pathname expansion: Wildcards replaced with what they represent. (Time to play with echo).
    4. Tilde expansion: home directories.
    5. Arithmetic expansion $((2+3*5)) Integer only.
    6. Brace expansion.
      1. {l,i,s,t}
      2. Similar to wildcards, but doesn't depend on what files exist.
        1. The wildcard *.cpp is expanded based on what .cpp files exist.
        2. The brace pattern {fred,barney,alice}.cpp expands to three specified files no matter which, or what other files, exist.
      3. Used in combination to create things. Textbook's date example; downloads.
    7. Parameter expansion,
      1. $NAME
      2. printenv
    8. Command substitution
      1. $(command)
      2. `command`
      3. Often useful to send a file list produced by one command to another.
    9. Quotes can be used to suppress expansion.
      1. Double quotes retain spaces and suppress expansions except parameter, arithmetic, and command (the dollar sign stuff).
      2. Single quotes suppress all.
      3. Useful if some benighted Windows user sends you a file with a space in the name.
    10. Escapes.
      1. Backward slash can escape a single character.
      2. Not special in single quotes.
      3. Use $'this' to use C-ish escape characters.
  3. Editing the command line, and tab completion.
  4. History.
    1. history command.
    2. Using history with arrow keys.
    3. history | egrep
    4. Reverse search with ctrl-R.
    5. History expansion with !
      1. !nnn
      2. Other odds and ends.