Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

What this section covers

  • Standard Unix streams, redirection and piping
  • How and why to direct script diagnostic messages to standard error, but function results to standard output
  • Argument defaulting
  • Automatically creating a log file for a script
  • Capturing standard output in a variable using backtick quoting

...

Redirection characters allow you to control the stream input or output

  • "<" read from
  • ">" write to (and it's cousin ">>" append to, when the destination is a file)

...

  • redirect standard output to a file, overwriting any exiting contents:
    echo "Output text" > out.txt
    echo "Output text" 1> out.txt
  • redirect standard output to a file, appending to any exiting contents:
    echo "More text" >> out.txt
    echo "More text" 1>> out.txt
  • redirect standard error output to a file, overwriting any exiting contents:
    echo "Error text" 2> err.txt
  • redirect standard error to standard output:
    ls ~ xxxx > ls.log 2>&1
  • redirect standard output to standard error:
    echo "Output that will go to standard error" 1>&2

...

For example, here's how to call the step_01.sh script so that its output goes to both the terminal and to a file.

Code Block
languagebash
~/workshop/step_01.sh helloWorld My name is Anna | tee step_01.log

See Piping in theĀ Linux Fundamentals page.

...