Skip to content

Practice: How the Shell Interprets Input

Module
How the Shell Interprets Input
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab container), GNU Bash 5.2.21; Ubuntu 24.04.4 LTS (course VM)
On this page

A teammate sends a handoff with a spaced label, several ticket files, and instructions intended for Bash. Before returning it, you need to prove which shell behavior the instructions require, preserve the label, pass it to a child process, and select only the requested tickets.

This practice combines B08.1–B08.5. Use the earlier lessons and documentation when needed. The result is evidence about what Bash passes to commands, rather than a memorized sequence.

Finish Bash and other shells through Wildcards and simple expansions. Keep your existing B04 lab directory.

Use only disposable ~/lab/b08/practice. Earlier exercises stay outside it. Run this setup once, or use Reset this lesson before repeating it:

Terminal window
mkdir -p ~/lab/b08/practice
cd ~/lab/b08/practice
touch ticket1.txt ticket2.txt ticket3.txt ticketA.txt readme.csv .draft.txt

The files are empty. Their names are the evidence needed for selection; you do not need to edit them or add contents.

Reach these five goals:

  1. Classify cd, ls, and if using Bash’s own inspection. Compare echo {4..6} under bash -c and sh -c, and explain the differing results without treating them as a terminal problem.
  2. Print two bracketed arguments: the phrase weekly review, then the literal text $HOME. The phrase must not split, and the dollar sign must remain visible.
  3. Store weekly review in handoff_label, export it, and prove that a child receives it as one argument. Let another child change its copy to checked, then prove that the parent still holds the original phrase.
  4. Inspect the command selected by cat with both type and command -v. Compare its containing directory with the course VM’s PATH in B08.4. Use your system’s result if it differs; do not replace your PATH to imitate the capture.
  5. Select only ticket1.txt and ticket3.txt using one bracket pattern. Generate the words batcha and batchb with brace expansion, then use quoted command substitution to pass checked copy as one argument. Finally print your home path using tilde expansion.

Predict before running goal 5: does generating batcha and batchb require files with those names? Explain your answer after checking the result.

  • Inspection distinguishes a builtin, the Ubuntu alias, and a keyword; the two shell invocations print different text.
  • One bracket pair encloses the entire spaced phrase, and another encloses literal $HOME.
  • A child prints the exported phrase, another prints its own changed value, and the parent still prints the phrase.
  • Two inspections identify the selected cat; your explanation connects the result to PATH order.
  • The wildcard selection prints exactly two ticket names. Generated labels, a single substituted phrase, and an expanded home path appear separately.

All solution output below is captured in the Ubuntu lab except the cat resolution, which is from the course VM’s B08.4 capture. Home paths vary by account. The practice does not change PATH or file contents.

Hint for goal 1

type can inspect a keyword without executing its syntax. The command string after -c belongs to the shell you start, so keep its outer single quotes.

Hint for goal 2

Use the fixed [%s]\n format from B08.2. Single quotes protect spaces and dollar signs literally.

Hint for goal 3

Export the assigned variable before starting the child. Single quotes around the command string delay expansion until the child reads it; double quotes inside preserve the child’s expanded value.

Hint for goal 4

Both inspection commands were introduced in B04.4. B08.4 explains how the directory in their answer fits the colon-separated list.

Hint for goal 5

The character set is 13, rather than a range that would also include 2. Brace expansion generates alternatives without checking files. Quotes around a substitution keep its printed phrase together.

Solution

Goal 1: distinguish shell behavior and command kinds

Terminal window
type cd
Captured output
cd is a shell builtin
Terminal window
type ls
Captured output
ls is aliased to `ls --color=auto'
Terminal window
type if
Captured output
if is a shell keyword
Terminal window
bash -c 'echo {4..6}'
Captured output
4 5 6
Terminal window
sh -c 'echo {4..6}'
Captured output
{4..6}

Bash expands the range; this lab’s Dash leaves it literal. The keyword inspection does not execute a conditional.

Goal 2: preserve two arguments

Terminal window
printf '[%s]\n' 'weekly review' '$HOME'
Captured output
[weekly review]
[$HOME]

The quotes group the phrase and suppress expansion of the dollar reference. They are removed before printf receives either string.

Goal 3: pass a value without sharing mutable state

Terminal window
handoff_label='weekly review'
export handoff_label
Terminal window
bash -c 'printf "[%s]\n" "$handoff_label"'
Captured output
[weekly review]
Terminal window
bash -c 'handoff_label=checked; printf "%s\n" "$handoff_label"'
Captured output
checked
Terminal window
printf '[%s]\n' "$handoff_label"
Captured output
[weekly review]

The second child changes only its own variable. Its command string uses the same sequential ; form explained in B08.3.

Goal 4: connect the selected file to PATH

Terminal window
type cat
Captured on the course VM
cat is /usr/bin/cat
Terminal window
command -v cat
Captured on the course VM
/usr/bin/cat

These are the course VM’s results, recorded in B08.4. /usr/bin is its fourth PATH entry. Other installations can select a different path, and Bash may report a remembered location as hashed after a prior invocation. Read the result from your own shell rather than assuming every installation has this exact PATH.

Goal 5: separate matching from generation

Terminal window
ls -1 ticket[13].txt
Captured output
ticket1.txt
ticket3.txt
Terminal window
printf '[%s]\n' batch{a,b}
Captured output
[batcha]
[batchb]
Terminal window
printf '[%s]\n' "$(printf '%s' 'checked copy')"
Captured output
[checked copy]
Terminal window
printf '[%s]\n' ~
Captured output
[/home/learner]

Only the first operation matches existing filenames. Braces generate the batch words without creating files; substitution supplies printed text. Tilde expands to this captured lab account’s home; your own path may differ.

Answer before opening each explanation.

1. Does changing a terminal application’s appearance change whether sh performs Bash brace expansion?

Answer

No. The shell reading the input determines its expansion rules. On this Ubuntu lab system, sh resolves to Dash.

2. What does an unclosed quote’s > prompt mean, and how do you abandon that line?

Answer

Bash needs more input to complete the syntax. Press Ctrl+C to cancel it and return to the ordinary prompt.

3. Why can a single-quoted \n still become a newline when used as a printf format?

Answer

Bash preserves the backslash and letter. printf then interprets that escape in its format. The shell did not create the newline while reading the single quotes.

4. Does export make a child’s later variable assignments change its parent?

Answer

No. Export selects a value for a subsequently started child’s environment. The child has its own copy, and its assignments do not write back to the parent.

5. Does type -a listing a builtin and two file paths mean all three run? What changes when you give an explicit command path?

Answer

No. It reports alternatives. An explicit command path containing a slash selects that path instead of searching PATH for a bare name.

6. How do globbing, brace expansion, and command substitution obtain their words?

Answer

Globbing matches existing names. Brace expansion generates alternatives without requiring files. Command substitution uses a command’s printed text and removes trailing newlines. They are separate expansion rules.

Remove the practice variable from this shell, then inspect the disposable directory:

Terminal window
unset handoff_label
cd ~/lab/b08
ls -AR practice

It contains only the six fixture files. Printing arguments and labels did not create batch files or alter the tickets.

Terminal window
rm -r ~/lab/b08/practice

Rerun Before you start to recreate the six empty files. If the directory is already absent, skip removal.

  • The selected shell determines syntax and expansion behavior.
  • Quotes determine whether text stays literal and which characters remain in one argument.
  • Export passes selected values to children without sharing their later assignments.
  • Command inspection connects a name to its selected implementation and PATH location.
  • Filename matching, brace generation, tilde expansion, and command substitution produce words by different rules.

Next, Standard streams explains how commands receive input and send ordinary output and diagnostics.