Linux · Systems fluency

How do pipes and redirection work in the shell?

Mediumpipesredirectionfile-descriptorsUpdated Oct 2026
Quiz this track

Short answer

Every process has standard input, output, and error as file descriptors 0, 1, and 2. Redirection changes where those descriptors point, and a pipe connects one process output to another process input.

The shell sets up descriptors before executing a command. Order matters: redirecting stderr to the current stdout and then changing stdout sends the two streams to different places.

sh
./deploy.sh > deploy.out 2>&1
./healthcheck.sh 2>/dev/null | tee health.log
sort access.log | uniq -c | sort -nr | head

Operators and descriptors

  • > truncates a file, while >> appends
  • 2> redirects stderr and 2>&1 points stderr at the current stdout
  • | connects stdout to the next command stdin
  • tee writes to a file and passes output along the pipeline
  • A here-document feeds multi-line input to a command

Each command in a pipeline runs in its own process, usually in a subshell. Variables set inside a pipeline stage may not persist in the parent shell. With pipefail enabled, a failure in any stage makes the pipeline fail.

A pipeline hides exit statuses unless you check them. Use set -o pipefail in scripts, and remember that SIGPIPE can terminate an early stage when a later consumer exits.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1What is the difference between > and >>?
  2. 2Why does the order of 2>&1 and > matter?
  3. 3What does pipefail change?
  4. 4Why might a variable set in a pipeline not be visible afterward?

Say it in the interview

Processes use file descriptors for stdin, stdout, and stderr. Redirection changes where those descriptors point, and a pipe connects stdout of one process to stdin of the next. Order matters when combining stdout and stderr. In scripts I enable pipefail because otherwise only the last command status is reported.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Linux mock interview and get scored feedback on depth, clarity and confidence.