CIS241

System-Level Programming and Utilities

Redirection and Piping

Erik Fredericks, frederer@gvsu.edu
Fall 2025

Based on material provided by Erin Carrier, Austin Ferguson, and Katherine Bowers

CIS241 | Fredericks | F25 | 8-Redirection-Piping
CIS241 | Fredericks | F25 | 8-Redirection-Piping

First, command chaining

Or, running multiple commands in a single line

Seperate with ; or &&

  • date ; who ; whoami ; echo "Hi there!"
  • date && who && whoami && echo "Hi there!"
    • Don't use a single & by mistake - that means run in the background!
CIS241 | Fredericks | F25 | 8-Redirection-Piping

A reference

https://dev.to/bowmanjd/bash-execution-tips-for-shell-jockeys-and-script-fabricators-5dan

Use || to execute one command only when the previous one fails.
Combine the above for conditional branching.
Use ; to join two commands when you want the second to execute no matter the result of the first one.
Use & to run the first job in the background while the next executes. Follow all with wait for a clean return to the command prompt

CIS241 | Fredericks | F25 | 8-Redirection-Piping

IO redirection

Standard streams

  • 0: stdin - Standard input (user typing)
  • 1: stdout - Standard output (print to terminal // output of program)
  • 2: stderr - Standard error (also prints to terminal)
CIS241 | Fredericks | F25 | 8-Redirection-Piping

IO redirection

Redirect stdout to file:

  • ls -la > dir_contents

Redirect stderr to file:

  • ls -la 2> errors

Redirect file to stdin:

  • sort < my_file.txt
CIS241 | Fredericks | F25 | 8-Redirection-Piping

Example:

Redirect stdout to file and stderr to stdout:

  • command >output 2>&1

Redirect stdout to file and stderr to another file:

  • command >output 2>output.err

Redirect stderr to the void:

  • command 2> /dev/null

command = echo "Hi" or eccccho "Hi"

CIS241 | Fredericks | F25 | 8-Redirection-Piping
CIS241 | Fredericks | F25 | 8-Redirection-Piping
(mario pipes)

Piping

CIS241 | Fredericks | F25 | 8-Redirection-Piping

Piping

top-right (streams)

bottom-corner (streams 2)

Send output of one command as input to another

command1 args | command2

  • cat file | sort
  • cat file | sort | uniq
  • cat file | sort | uniq > output.txt
CIS241 | Fredericks | F25 | 8-Redirection-Piping

echo "hello" & echo "world"

note: the first will log to f

create a file with random words do the above