You type echo hello into a terminal and see hello. Something must read your text, recognize echo, and decide what to print. That something is the shell. Without its command loop, the text is just text.
Build the shell's first decision loop. Handle echo, pwd, cd, and exit, and keep the chosen directory between commands. The judge models that directory as text rather than changing real files.
Input: A sequence of command lines. Read until exit or the end of input. Ignore blank lines and split other lines by whitespace.
Output: Print one line for each echo, pwd, or unknown command. cd is silent. exit stops immediately and prints nothing.
Input:
echo hello world exit
Output:
hello world
Explanation: echo is the instruction; hello and world are its arguments. The shell prints those arguments, then exit ends the loop silently.
Input:
pwd exit
Output:
OK
Explanation: No directory has been changed yet, so pwd prints the model's initial value, OK.
Input:
cd /tmp pwd exit
Output:
/tmp
Explanation: cd stores /tmp without printing. The next pwd reads that stored value: the loop remembers state between lines.
Input:
echo alpha echo beta exit
Output:
alpha beta
Explanation: The loop handles both commands in order. Reading one line does not end the session.
Input:
fakecommand exit
Output:
fakecommand: command not found
Explanation: An unrecognized name produces a useful error instead of being treated as echo or ending the loop.
Paste this into Custom Input and run your solution.
cd /notes echo ready pwd exit echo too late
Why is there no output for cd, exit, or the last echo?
Expected output:
ready /notes
cd only changes stored state. echo ready prints once, then pwd reads /notes. exit stops the loop, so the final line is never processed.
What single piece of state must survive between cd and the next pwd?
You are building the decision loop behind command-line tools. The next level separates a program name from its arguments and models finding and running that program.
Keep the parent command loop ready while reporting each program's result.
hello world