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