Practice: How the Shell Interprets Input
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.
Before you start
Section titled “Before you start”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:
mkdir -p ~/lab/b08/practicecd ~/lab/b08/practicetouch ticket1.txt ticket2.txt ticket3.txt ticketA.txt readme.csv .draft.txtThe files are empty. Their names are the evidence needed for selection; you do not need to edit them or add contents.
Your task
Section titled “Your task”Reach these five goals:
- Classify
cd,ls, andifusing Bash’s own inspection. Compareecho {4..6}underbash -candsh -c, and explain the differing results without treating them as a terminal problem. - 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. - Store
weekly reviewinhandoff_label, export it, and prove that a child receives it as one argument. Let another child change its copy tochecked, then prove that the parent still holds the original phrase. - Inspect the command selected by
catwith bothtypeandcommand -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. - Select only
ticket1.txtandticket3.txtusing one bracket pattern. Generate the wordsbatchaandbatchbwith brace expansion, then use quoted command substitution to passchecked copyas 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.
Expected evidence
Section titled “Expected evidence”- 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
Section titled “Solution”Solution
Goal 1: distinguish shell behavior and command kinds
type cdcd is a shell builtintype lsls is aliased to `ls --color=auto'type ifif is a shell keywordbash -c 'echo {4..6}'4 5 6sh -c 'echo {4..6}'{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
printf '[%s]\n' 'weekly review' '$HOME'[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
handoff_label='weekly review'export handoff_labelbash -c 'printf "[%s]\n" "$handoff_label"'[weekly review]bash -c 'handoff_label=checked; printf "%s\n" "$handoff_label"'checkedprintf '[%s]\n' "$handoff_label"[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
type catcat is /usr/bin/catcommand -v cat/usr/bin/catThese 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
ls -1 ticket[13].txtticket1.txtticket3.txtprintf '[%s]\n' batch{a,b}[batcha][batchb]printf '[%s]\n' "$(printf '%s' 'checked copy')"[checked copy]printf '[%s]\n' ~[/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.
Recall check
Section titled “Recall check”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.
Reset this lesson
Section titled “Reset this lesson”Remove the practice variable from this shell, then inspect the disposable directory:
unset handoff_labelcd ~/lab/b08ls -AR practiceIt contains only the six fixture files. Printing arguments and labels did not create batch files or alter the tickets.
rm -r ~/lab/b08/practiceRerun 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.
Further reading
Section titled “Further reading”- On your system:
man bash,help type,help printf, andhelp export. - GNU Bash: Shell Expansions, Environment, and Command Search and Execution.