A pipe (|) connects the output of one command directly as input to another command, allowing commands to work together smoothly.
Click to reveal answer
beginner
Why do we use pipes to chain commands?
Pipes let us combine simple commands to create powerful workflows without creating temporary files, making tasks faster and easier.
Click to reveal answer
intermediate
How does chaining commands with pipes improve efficiency?
It saves time and system resources by passing data directly between commands, avoiding extra steps like saving and reading files.
Click to reveal answer
beginner
Give an example of a simple command chain using pipes.
Example: ls -l | grep ".txt" | sort lists files, filters only .txt files, then sorts them.
Click to reveal answer
intermediate
What happens if a command in a pipe chain fails?
The following commands still run, but they may get no input or wrong input, so the whole workflow might not work as expected.
Click to reveal answer
What does the pipe symbol (|) do in Linux commands?
ASends output of one command as input to another
BDeletes files
CRuns commands in the background
DChanges file permissions
✗ Incorrect
The pipe (|) connects commands by sending output from one as input to the next.
Why is chaining commands with pipes useful?
AIt creates temporary files
BIt makes commands run slower
CIt changes the command prompt
DIt allows combining simple commands into workflows
✗ Incorrect
Pipes let you combine commands to build workflows without extra files.
Which of these is a valid pipe chain example?
Amkdir newfolder | cd newfolder
Bls -l > file.txt | rm file.txt
Ccat file.txt | grep "hello" | sort
Decho "hello" & ls
✗ Incorrect
Option C correctly uses pipes to send output between commands.
What happens if the first command in a pipe fails?
AThe next commands get no or bad input
BThe whole chain stops immediately
CThe system restarts
DThe commands run twice
✗ Incorrect
If the first command fails, the next commands may receive no input and produce no useful output.
Pipes help avoid which of the following?
ATyping commands
BUsing temporary files to pass data
CRunning commands
DSaving files permanently
✗ Incorrect
Pipes pass data directly between commands, so temporary files are not needed.
Explain in your own words why pipes are useful for chaining commands in Linux.
Think about how you can connect simple tasks to do bigger jobs without saving files.
You got /4 concepts.
Describe what happens when you use a pipe between two commands.
Imagine passing a note from one friend to another to continue a task.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using pipes (|) in Linux command lines?
easy
A. To send the output of one command as input to another command
B. To run two commands at the same time independently
C. To save the output of a command to a file
D. To repeat a command multiple times automatically
Solution
Step 1: Understand what pipes do
Pipes take the output from one command and pass it directly as input to the next command.
Step 2: Compare options with pipe behavior
Only To send the output of one command as input to another command describes this behavior correctly; others describe different features.
Final Answer:
To send the output of one command as input to another command -> Option A
Quick Check:
Pipes connect commands by passing output as input [OK]
Hint: Pipes pass output to next command input [OK]
Common Mistakes:
Thinking pipes run commands in parallel independently
Confusing pipes with file redirection
Assuming pipes repeat commands
2. Which of the following is the correct syntax to use a pipe between two commands in Linux?
easy
A. command1 | command2
B. command1 >| command2
C. command1 || command2
D. command1 & command2
Solution
Step 1: Identify the pipe symbol
The pipe symbol is a single vertical bar: |.
Step 2: Match syntax with pipe usage
Only command1 | command2 uses the correct pipe syntax between two commands.
Final Answer:
command1 | command2 -> Option A
Quick Check:
Pipe syntax uses a single vertical bar [OK]
Hint: Pipe uses single vertical bar | between commands [OK]
Common Mistakes:
Using double pipes || which means OR condition
Using >| which is invalid syntax
Using & which runs commands in background
3. What will be the output of the following command?
echo "apple banana cherry" | tr ' ' '\n' | sort
medium
A. apple banana cherry
B. apple
banana
cherry
C. banana
apple
cherry
D. cherry banana apple
Solution
Step 1: Break down the pipeline
echo "apple banana cherry" outputs the string. tr ' ' '\n' replaces spaces with new lines, so words become separate lines. sort sorts these lines alphabetically.
Step 2: Determine sorted output
Words sorted alphabetically are: apple, banana, cherry, each on its own line.
Final Answer:
apple
banana
cherry -> Option B
Quick Check:
Pipe transforms and sorts words line by line [OK]
Hint: Trace each pipe step to find final output [OK]
Common Mistakes:
Ignoring that tr changes spaces to new lines
Assuming sort does not change order
Thinking output stays on one line
4. Identify the error in this pipeline command:
cat file.txt | grep 'error' | | sort
medium
A. sort command cannot be used with pipes
B. Missing filename after cat
C. grep command is misspelled
D. Extra pipe symbol causes syntax error
Solution
Step 1: Check the pipeline syntax
The command has two pipe symbols | | in a row, which is invalid syntax.
Step 2: Validate other parts
cat file.txt is correct, grep 'error' is correct, and sort can be used with pipes.
Final Answer:
Extra pipe symbol causes syntax error -> Option D
Quick Check:
Double pipes cause syntax errors in pipelines [OK]
Hint: Look for consecutive pipes causing syntax errors [OK]
Common Mistakes:
Ignoring extra pipe and blaming grep or cat
Thinking sort can't be piped
Assuming missing filename error
5. You want to count how many unique users are currently logged in by chaining commands. Which pipeline correctly achieves this?
hard
A. who | cut -d ' ' -f1 | uniq | sort | wc -l
B. who | sort | uniq | cut -d ' ' -f1 | wc -l
C. who | cut -d ' ' -f1 | sort | uniq | wc -l
D. who | uniq | cut -d ' ' -f1 | sort | wc -l
Solution
Step 1: Extract usernames from who output
cut -d ' ' -f1 extracts the first word (username) from each line.
Step 2: Sort and find unique usernames
sort orders usernames so uniq can count unique ones correctly.
Step 3: Count unique usernames
wc -l counts the number of unique usernames output by uniq.
Final Answer:
who | cut -d ' ' -f1 | sort | uniq | wc -l -> Option C
Quick Check:
Extract, sort, uniq, then count lines for unique users [OK]
Hint: Sort before uniq to count unique items correctly [OK]
Common Mistakes:
Using uniq before sort causing wrong counts
Cutting fields after uniq breaking logic
Sorting after uniq which is ineffective
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.