Connect Output and Save Text
Connect Output and Save Text
Suppose a teacher wants a class list in alphabetical order and wants to save the sorted version. We can give each tool one small job: one produces the names, another sorts them, and the shell sends the result into a file. You do not need one large program that knows every part of the task.
A pipe, written |, connects one command's standard output to the next command's standard input. Redirection, such as >, connects an input or output stream to a file instead. Standard input and standard output are the usual channels for data entering and leaving a command; the terminal is only one possible destination.
What you’ll learn
- Explain the operation and choose it for the stated school task.
- Read the command arguments and check the result rather than relying on silence alone.
- Apply the worked task and recognize this boundary: Standard error is a separate stream; a normal pipe does not automatically include it. The shell opens redirection destinations before commands run, so never use cat file > file to copy a file onto itself.
Follow the names from producer to saved list
Produce
printf writes the unsorted names to standard output.
Saira, DiyaTransform
The pipe delivers those bytes to sort through standard input.
Diya, SairaSave
The shell directs sorted output into the destination, replacing existing contents.
students.txt
Sort a class list and save the result
Prepare the example
Work in a fresh folder so that students.txt is a disposable destination. The example creates its own input text; there is no earlier file to prepare. Keep the entire first line together, including the pipe and redirection, because the shell uses them to build the connection between commands.
Bash · Linux terminal
printf 'Saira\nDiya\n' | sort > students.txt
cat students.txtExpected result
Diya
Sairaprintf produces Saira followed by Diya on separate lines. The pipe delivers those lines to sort, which puts them in order. Then > students.txt directs the sorted output into a file. cat students.txt is a separate check: it lets us see what was actually saved.
There is an important catch with >: the shell opens and truncates an existing destination before the command runs. That is why reading a file and redirecting into that same file is unsafe. Use a different destination when transforming existing data. Also remember that a normal pipe carries standard output; diagnostic messages on standard error use a separate stream.
Try it, then check the worked answer
Your task: Why is cat students.txt > students.txt unsafe?
Work in a fresh folder so that students.txt is a disposable destination. The example creates its own input text; there is no earlier file to prepare. Keep the entire first line together, including the pipe and redirection, because the shell uses them to build the connection between commands.
Bash · complete example for this question
printf 'Saira\nDiya\n' | sort > students.txt
cat students.txtExpected result
Diya
SairaWorked answer
The shell truncates the destination before cat reads it.
In cat students.txt > students.txt, the shell prepares the output destination first. That empties the very file cat is about to read. There is no protected intermediate copy hiding inside the command. Save transformed content to another path, inspect it, and only then decide whether it should replace the original.
Check your decision
A pipe connects programs; redirection opens a file
Use a pipe when one command should consume another command's output. Use redirection when the stream should go to or come from a file. Before typing >, check whether the destination already holds something worth keeping. >> appends instead, but appending is useful only when that is the operation you actually want.
Reaching the recap and completing the quizzes are separate milestones.