Bird
Raised Fist0
Linux CLIscripting~10 mins

Pipe operator (|) in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Pipe operator (|)
Command 1 runs
↓
Output of Command 1
↓
Pipe operator (|) passes output
↓
Command 2 receives input
↓
Command 2 runs and outputs result
The pipe operator (|) takes the output of the first command and sends it as input to the second command.
Execution Sample
Linux CLI
echo "apple banana cherry" | tr ' ' '\n'
This command sends a string to 'tr' which replaces spaces with new lines, splitting words vertically.
Execution Table
StepCommandActionOutputNext Input
1echo "apple banana cherry"Outputs string 'apple banana cherry'apple banana cherryPassed to tr
2tr ' ' '\n'Replaces spaces with new linesapple banana cherryDisplayed on screen
3EndNo more commandsProcess completeNo input
💡 Pipe ends after last command outputs result to screen
Variable Tracker
VariableStartAfter Step 1After Step 2Final
Output Streamemptyapple banana cherryapple banana cherryapple banana cherry
Key Moments - 2 Insights
Why does the output of the first command become the input of the second?
Because the pipe operator (|) connects the output stream of the first command directly to the input stream of the second command, as shown in execution_table step 1 and 2.
What happens if the first command produces no output?
The second command receives no input and may produce no output or behave differently, since the pipe passes exactly what the first command outputs.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after step 1?
A"apple\nbanana\ncherry"
Bempty
C"apple banana cherry"
D"apple, banana, cherry"
💡 Hint
Check the 'Output' column in row for step 1 in execution_table
At which step does the pipe operator pass data to the next command?
AStep 3
BStep 1
CStep 2
DNo step
💡 Hint
Look at the 'Next Input' column in execution_table for step 1
If the first command output was empty, how would the final output change?
ASecond command outputs nothing
BSecond command outputs original string
CSecond command outputs error
DNo change
💡 Hint
Refer to key_moments about empty output from first command
Concept Snapshot
Pipe operator (|) connects commands.
Output of first command becomes input of second.
Used to chain commands simply.
Example: echo "a b" | tr ' ' '\n'
Splits words into lines.
Full Transcript
The pipe operator (|) in Linux command line takes the output from one command and sends it as input to the next command. For example, 'echo "apple banana cherry" | tr ' ' '\n'' sends the string from echo to tr, which replaces spaces with new lines. Step 1 runs echo and outputs the string. Step 2 runs tr, receiving that string as input and outputs each word on a new line. The pipe allows chaining commands easily by passing data between them without saving to files. If the first command outputs nothing, the second command receives no input and may output nothing. This flow helps build powerful command sequences.

Practice

(1/5)
1. What does the pipe operator | do in Linux command line?
easy
A. Sends the output of one command as input to another command
B. Runs two commands simultaneously
C. Redirects output to a file
D. Comments out a line in a script

Solution

  1. Step 1: Understand the pipe operator function

    The pipe operator | takes the output from the command on its left side.
  2. Step 2: Connect output to input of next command

    This output is then passed as input to the command on the right side of the pipe.
  3. Final Answer:

    Sends the output of one command as input to another command -> Option A
  4. Quick Check:

    Pipe operator = send output to next command [OK]
Hint: Pipe connects output to input of next command [OK]
Common Mistakes:
  • Thinking pipe runs commands in parallel
  • Confusing pipe with output redirection >
  • Assuming pipe comments code lines
2. Which of the following is the correct syntax to use the pipe operator to list files and then count them?
easy
A. ls & wc -l
B. ls > wc -l
C. ls || wc -l
D. ls | wc -l

Solution

  1. Step 1: Identify correct pipe syntax

    The pipe operator is |, which connects commands by passing output to input.
  2. Step 2: Check command usage

    ls | wc -l lists files and counts lines, which is correct syntax.
  3. Final Answer:

    ls | wc -l -> Option D
  4. Quick Check:

    Pipe syntax uses | between commands [OK]
Hint: Use single | to pipe commands, not > or & [OK]
Common Mistakes:
  • Using > which redirects output to file
  • Using & which runs commands in background
  • Using || which is logical OR, not pipe
3. What is the output of the command: echo -e "apple\nbanana\ncherry" | grep "an"?
medium
A. apple cherry
B. apple banana cherry
C. banana
D. No output

Solution

  1. Step 1: Understand the echo command output

    echo -e "apple\nbanana\ncherry" outputs three lines: apple, banana, cherry.
  2. Step 2: Apply grep filter with "an"

    grep "an" filters lines containing "an", which matches only "banana".
  3. Final Answer:

    banana -> Option C
  4. Quick Check:

    grep filters lines with 'an' = banana [OK]
Hint: grep filters lines containing the pattern [OK]
Common Mistakes:
  • Expecting all words printed on one line
  • Ignoring newline characters in echo
  • Thinking grep matches partial words only
4. Identify the error in this command: cat file.txt | | grep "error"
medium
A. No error, command runs fine
B. Extra pipe operator causes syntax error
C. grep command is misspelled
D. Missing filename after cat

Solution

  1. Step 1: Analyze the pipe usage

    The command has two pipe operators | | consecutively, which is invalid syntax.
  2. Step 2: Understand correct pipe usage

    Only one pipe operator should separate commands; extra pipes cause errors.
  3. Final Answer:

    Extra pipe operator causes syntax error -> Option B
  4. Quick Check:

    Double pipes cause syntax error [OK]
Hint: Use single | between commands, no doubles [OK]
Common Mistakes:
  • Ignoring extra pipe and expecting output
  • Thinking missing filename is the error
  • Assuming grep is misspelled
5. You want to find how many lines in log.txt contain the word "fail" but ignore case. Which command correctly uses pipes to do this?
hard
A. cat log.txt | grep -i fail | wc -l
B. grep fail log.txt | wc -l
C. cat log.txt > grep -i fail | wc -l
D. grep -i fail | cat log.txt | wc -l

Solution

  1. Step 1: Use cat to output file content

    cat log.txt outputs the file content to pipe.
  2. Step 2: Use grep with -i to ignore case

    grep -i fail filters lines containing "fail" ignoring case.
  3. Step 3: Count lines with wc -l

    wc -l counts the filtered lines.
  4. Final Answer:

    cat log.txt | grep -i fail | wc -l -> Option A
  5. Quick Check:

    Pipe commands to filter and count lines [OK]
Hint: Pipe cat to grep -i then to wc -l for case-insensitive count [OK]
Common Mistakes:
  • Using > instead of | breaks pipe chain
  • Placing commands in wrong order
  • Omitting -i for case-insensitive search
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard