Linux Foundations / Predict What the Shell Does

Build a short pipeline and check every stage

Lesson 12 of 37. Open to read. No account needed.

A count is useful only when you know what was counted. A command can report zero because its input was empty, because an earlier command failed to supply that input, or because your selection removed every record. A plausible number at the end of a command line does not settle which explanation is correct.

You will build a short delivery-list pipeline, inspect its stages, and save a complete copy while counting the data. Then you will observe a deliberate upstream failure and explain why the downstream count and the pipeline's overall status do not prove that every stage succeeded.

1. Start from a fresh, owned delivery list

Complete Redirect output and errors deliberately first. Work as the ordinary Bash user in the checked Ubuntu guest. Use the already taught identity and command-origin checks. Run commands one line at a time, read each result, and stop after unexpected errors before continuing. The intentional missing-input failure will be identified before it runs.

id -u
id -un
ps -p $ -o comm=
type printf set mkdir cat wc head tail tee ls rm rmdir

Require a nonzero user ID, the expected username and Bash. printf and set must be Bash builtins; the external utilities must have their expected installed origins. Preserve an understood color-only listing alias, and reconcile an unexpected alias, function or location before invoking the name.

Use the taught read-only option inspection:

set -o

Require pipefail and errexit to be off. With pipefail off, the pipeline's overall status comes from its final command; we will examine that consequence later. errexit off keeps the deliberately unsuccessful observations within the normal interactive exercise. Stop and reconcile a different baseline without changing either option here. No profile or shell setting is edited.

This task owns ~/linux-practice/build-and-check-pipelines/. Inspect parents and the prospective exercise name with the known no-trailing-slash checks:

ls -ld ~
ls -ld ~/linux-practice
ls -ld ~/linux-practice/build-and-check-pipelines

Existing parents must be ordinary directories, shown with leading d, owned by the expected account. Reject links, unexpected ownership or an unrecognized path. The exercise name must be absent for this fresh-start procedure; use the resume section if it already exists.

Only after confirming absence, create and inspect the new directory:

mkdir ~/linux-practice/build-and-check-pipelines
ls -ld ~/linux-practice/build-and-check-pipelines

Require the expected ordinary directory and owner. Enter and verify it before creating any input:

cd ~/linux-practice/build-and-check-pipelines
pwd
ls -a

Require the recorded shared-root path followed by /build-and-check-pipelines and an empty directory containing only . and ... Stop if location or inventory differs.

Six complete records, supplied inline

The already taught printf '%s\n' format prints each following string with a terminating newline, reusing the format for the next argument. Six nonempty quoted strings therefore supply six newline-terminated records. There is no blank record and no missing final newline in this fixture.

The taught > creates the new source file. deliveries.txt must be absent before running this command; do not redirect over an existing or linked input:

printf '%s\n' 'DEL-01 library' 'DEL-02 studio' 'DEL-03 clinic' 'DEL-04 workshop' 'DEL-05 cafe' 'DEL-06 school' > deliveries.txt

Inspect the saved source before connecting any commands:

ls -ld -- deliveries.txt
cat deliveries.txt
ls -a

Require an ordinary regular file with leading -, one link and your recorded owner. Its whole content must be exactly these six records, in this order:

DEL-01 library
DEL-02 studio
DEL-03 clinic
DEL-04 workshop
DEL-05 cafe
DEL-06 school

The input remains unchanged throughout the task. deliveries-copy.txt is a separate future output, and missing-deliveries.txt is intentionally absent. The independent task will create afternoon.txt and afternoon-copy.txt. These four real files are the entire planned fixture; no missing-input file should be created to make a later diagnostic disappear.

2. Connect stdout to the next command's stdin

A pipe, written |, connects the standard output of the command on its left to the standard input of the command on its right. Bash supplies the connection. The bar is shell syntax, not an argument passed to either command. A pipeline is a sequence of commands connected this way; each command is a stage.

The stages can run at the same time while data flows between them. A pipe does not first save a hidden report file and then invoke the next stage. Standard error remains a separate channel; an ordinary | does not put diagnostics into the next command's data input.

wc counts properties of its input. Here, wc -l counts newline characters. With a filename operand it reads that file and includes its name in the result; with no filename operand it reads stdin. A blank line contains a newline and counts. A final text fragment without a terminating newline is not another newline, even if it looks like a final record on screen.

First check the individual counter against the six supplied records:

wc -l -- deliveries.txt

The result should report six newlines for deliveries.txt. An illustrative presentation is 6 deliveries.txt; spacing can vary. For this deliberately constructed input, six newlines correspond to its six complete records. That correspondence is part of the fixture, not a universal definition of a business record.

Now predict the pipe's data and final display:

cat deliveries.txt | wc -l

cat writes all six source records to stdout. The pipe supplies those bytes to wc on stdin. wc -l prints the count, normally 6 with no filename because it was not given a file operand. The source has not been changed.

For this simple count, wc -l < deliveries.txt is also sufficient and uses the previously taught input redirection. We use the two-stage form to see which output becomes which input before adding a useful branching stage.

3. Inspect a selection before adding another stage

head -n 4 outputs the first four lines of its input; tail -n 2 outputs the last two. Here -n introduces the number of lines, and the following positive integer is the requested amount. With a filename these tools read that file; without one they read stdin. The supplied -- ends option parsing before a filename, as in the earlier GNU tools.

Inspect each selector separately and predict its exact record IDs first:

head -n 4 -- deliveries.txt

Expect DEL-01 through DEL-04, with their complete corresponding text. Then inspect the end of the entire original source:

tail -n 2 -- deliveries.txt

Expect DEL-05 cafe and DEL-06 school. Now predict this connection:

head -n 4 -- deliveries.txt | tail -n 2

The second stage receives only the first stage's four lines. Its “last two” are therefore DEL-03 clinic and DEL-04 workshop, not the last two lines of the original six-line source. The scope of a stage's input determines what its selection means. If the result differs, inspect the source and the first stage before changing the second stage's number.

These are read-only selections. They neither rewrite the source nor create a saved selection file.

4. Save the complete data while counting it

tee reads stdin and writes the same incoming data to stdout and to each named file. In this lesson it has one named output. tee deliveries-copy.txt thus creates a side copy while passing the stream onward to the counter.

By default, tee can overwrite an existing named output without asking. It opens that file itself; Bash's noclobber option is not an overwrite guard for tee. We use only a fresh, distinct output, never the source filename. A pipeline can read and write concurrently, so using the same input and output can truncate data before it has been read. Do not try that as an experiment.

Inspect the source's identity and contents again, and inspect the inventory:

ls -ld -- deliveries.txt
cat deliveries.txt
ls -a

Require the unchanged six-line source and deliveries-copy.txt absent. No link, existing unknown file or alternate input name is an acceptable output. Only after these checks, predict the copy's contents and final count, then run:

cat deliveries.txt | tee deliveries-copy.txt | wc -l

The expected count is six. The first pipe carries the source records into tee; the second carries those same records into wc. tee does not replace its stdout with a filename or with a count.

A three-stage pipeline sends cat's stdout into tee's stdin, then tee's stdout into wc's stdin. Tee also writes all six delivery records to the separate deliveries-copy.txt. Standard error stays outside the data path, and source, copy and final count are separate checkpoints.

Follow the arrows as data channels. The file branch preserves the whole incoming stream, while the terminal receives the counter's result. The stderr arrows remain separate: their display order relative to the count is not a claim about which stage ran first.

Inspect the new file and compare complete contents, not just counts:

ls -ld -- deliveries-copy.txt
cat deliveries.txt
cat deliveries-copy.txt
wc -l -- deliveries.txt deliveries-copy.txt

Require the copy to be an ordinary owned single-link file. Both texts must match every supplied record, in order. wc should report six for each file and, because two files were supplied, a total of twelve. The total is a sum of two inspections, not evidence that one source grew to twelve records. Two different six-line files could have equal counts, so the full content comparison is essential.

A tee file-writing error is also a reason to stop even if downstream output looks plausible. A final count does not prove the copy was successfully saved.

5. A zero count can hide an earlier failure

First inspect ls -a and confirm missing-deliveries.txt is absent. The next pipeline intentionally asks cat to read that nonexistent input. It changes no files. Predict separately what reaches wc and what reaches the terminal as a diagnostic.

We will also inspect one overall status immediately afterward. $? is a read-only special Bash parameter containing the status of the most recently completed foreground pipeline or command. Double quotes allow that value to expand as one argument. The following printf prints it using the already taught %s format; it does not assign a variable.

Read the status before running an intervening command: even a successful inspection command would replace the value. Run the pipeline, then the following printf as the very next command:

cat missing-deliveries.txt | wc -l
printf 'Pipeline status: %s\n' "$?"

cat should report a missing-input diagnostic on its stderr, still connected to the terminal. It supplies no file data to the pipe. wc can successfully count that empty input and print zero. With our checked pipefail-off baseline, a successful final wc gives the overall pipeline status 0, even though the earlier cat failed. If the final counter itself fails, do not assume this illustrative status applies.

The count and diagnostic may appear in either relative order. Their positions on the screen do not change which channel carried each one. Record the observations separately. The status-printing printf is itself another command, so a later $? would describe that newer command rather than the original pipeline.

Now isolate the suspected upstream stage, without creating the missing file:

cat missing-deliveries.txt

This deliberately unsuccessful read should expose the same missing-input problem directly. Then check the known good source with its own counter:

wc -l < deliveries.txt

It should still report six. These checks distinguish a broken source request from genuinely empty intended data. Keep the original input and saved copy unchanged. The lesson uses the declared default status rule; it does not change pipefail or teach a shell script's error-handling policy.

Diagnose one stage at a time

ObservationWhat it might meanSafe next check
The count is zero and an upstream error is visible.No intended data reached the counter.Inspect the named source and run its read stage separately.
The count is zero without a diagnostic.The source may be empty or may contain text without newlines.Read the actual source and check how its records were created. Zero newlines does not necessarily mean zero bytes.
The count exceeds the visible intended records.Blank lines or a different input may be included.Compare every source line with the supplied fixture and check newline boundaries.
A last-two selection returns unexpected IDs.It selected from an earlier stage's output, not the complete original file.Inspect that earlier stage before changing the final selector.
A count looks correct, but the tee copy differs or is absent.File output may have failed or a wrong path was used.Read the diagnostic and inspect the exact copy entry and its complete contents.
Overall status is zero despite an earlier failure.Under the checked rule, the last stage succeeded.Inspect upstream diagnostics and stages; the one overall number is insufficient.

Independent task: four afternoon deliveries

Keep the guided source and copy unchanged. Recheck the exercise path, directory identity and current inventory. Require afternoon.txt and afternoon-copy.txt absent before creating either. The four new records are supplied using the same explained format:

printf '%s\n' 'PM-01 museum' 'PM-02 garden' 'PM-03 theatre' 'PM-04 depot' > afternoon.txt

Inspect this new input before attempting the pipeline:

ls -ld -- afternoon.txt
cat afternoon.txt

Require the ordinary owned single-link file and these four exact, newline- terminated records in order:

PM-01 museum
PM-02 garden
PM-03 theatre
PM-04 depot

Before opening the answer, construct a pipeline that passes the entire new input through tee, saves a separate afternoon-copy.txt, and counts the forwarded newlines. Predict its final number and which four records the copy must contain. Keep the output distinct and fresh. Do not reuse the guided copy or use afternoon.txt as tee's output.

After your attempt, verify both new file contents and the count. Explain why counting four does not, on its own, prove that the correct four records were copied. Also predict the record IDs from taking the first three afternoon lines and then the last two of that intermediate result.

Success: the new copy matches all four supplied records, the counter reports four, the guided six-line source/copy remain unchanged, and your stage explanation identifies which input each command receives. No ongoing process is expected after the foreground pipeline returns to the prompt.

Graduated hints

  1. Use the same whole-data path as the guided pipeline. A selector would change which records reach the saved copy and the count.
  2. The left stage reads afternoon.txt. The middle stage's filename is the new output afternoon-copy.txt. The last stage counts stdin rather than opening another source file.
  3. Join cat, tee and wc -l with two | operators. Check the saved text separately after the pipeline; a final number is only one piece of evidence.

Answer and Reasoning

From the verified exercise directory, with the supplied new input checked and afternoon-copy.txt still absent, one valid pipeline is:

cat afternoon.txt | tee afternoon-copy.txt | wc -l

The final output should report four. cat sends the four records, tee writes all four to its fresh file and forwards them unchanged, and wc counts their four terminating newlines. Inspect the copy and compare the whole input:

ls -ld -- afternoon-copy.txt
cat afternoon.txt
cat afternoon-copy.txt
wc -l -- afternoon.txt afternoon-copy.txt
cat deliveries.txt
cat deliveries-copy.txt

Require the expected ordinary owned single-link copy. Each afternoon file should have four newlines; their two-file total is eight. Compare every record because equal counts can describe different contents. The original two guided files must still match all six DEL records. Different read-only inspection choices are acceptable if they establish these same contents, boundaries and unchanged inputs without introducing another output or overwriting a source.

For the selection prediction, the first three afternoon records are PM-01, PM-02 and PM-03. The last two of that intermediate set are PM-02 garden and PM-03 theatre, not PM-03 and PM-04 from the whole original input.

For the failure check, the absent input made the first stage fail while the counter successfully processed an empty stream. Under the checked pipefail-off rule, the final stage supplied the overall zero status. This explains why checking source data, diagnostics and the saved copy adds evidence that the final count or status alone cannot provide.

Cleanup after all comparisons

Complete the independent work before removing any source or copy. Inspect the shared root and exercise entries, rejecting links or unexpected ownership:

ls -ld ~/linux-practice
ls -ld ~/linux-practice/build-and-check-pipelines

Enter and verify separately:

cd ~/linux-practice/build-and-check-pipelines
pwd

Require the exact recorded exercise path before inspecting removal targets:

ls -a
ls -ld -- deliveries.txt deliveries-copy.txt afternoon.txt afternoon-copy.txt

Require exactly the four known regular single-link files owned by your user, plus dot entries. Verify any content you cannot identify; leave unrecognized entries untouched. missing-deliveries.txt remains absent and is not a target.

The shown y/n responses assume English confirmations. Establish the actual program's documented responses first, and stop if the language or response convention is unclear. Only after the target checks, use named interactive removal:

rm -i -- deliveries.txt deliveries-copy.txt afternoon.txt afternoon-copy.txt

Approve only the expected owned files. If stopping, decline the current and every remaining prompt; one refusal does not cancel the later prompts. Inspect ls -a and require only . and .. before directory removal.

Return to the shared root and verify it in a separate step:

cd ~/linux-practice
pwd

Require the recorded shared-root path. Then inspect the exercise entry itself:

ls -ld -- build-and-check-pipelines

Only for the recognized owned empty directory:

rmdir -- build-and-check-pipelines
ls -a

Retain the shared practice root and all unrelated lesson directories. No shell option or system resource needs restoring.

Resume without replacing evidence

After an interruption, recheck the ordinary guest, command origins, read-only option states, parent identities and exact location. Inspect existing files and contents to identify the last completed step. Repeating a read-only count is different from repeating a tee command that overwrites its output.

Do not run a new-copy command over an existing file merely to see the same number again. First inspect whether that known copy already supplies the expected evidence. To repeat a copy operation deliberately, inspect and remove only that recognized owned copy with the taught named interactive removal, then confirm its name absent before recreating it. Preserve the source.

For a complete restart, remove only the recognized task files actually created, using the same identity, location and prompt checks. Leave unknown entries untouched. Rebuild a fresh empty exercise directory and recreate the supplied inputs; a guessed source or a missing final newline changes the counting task.

Sources and other study views

These bounded commands target the course's Bash 5.2 and GNU Coreutils 9.4 baseline. The upstream Coreutils manual can describe newer versions; it does not make every newer option or changed default part of this exercise.

Use Revision to reconstruct the checks, or Quick Reference to retrieve the taught patterns.

Study this course with an account

Course access adds revision notes, the Quick Reference, saved progress, bookmarks and your own notes.

INR 99 plus 18% GST. 90 days of online access. No auto-renewal.

See course access

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy