The line as typed
Consider the line above. It contains two commands joined by a pipe, and the second command sends its output to a file. Nothing in the text says what runs first, so bash must read the line in stages. It first reads characters until it reaches a newline that is not inside quotes. Then it breaks the text into tokens. Some tokens are words, and some are operators such as the pipe symbol and the greater-than sign. The quoted strings stay as single tokens even when they contain spaces, which is why the value 'x y' remains one unit.
After tokenizing, bash parses the tokens into a structure. The line is a pipeline of two simple commands. The first simple command is printf with three words. The second is wc with the option -c and no file names, so it reads standard input, and it carries a redirection that sends standard output to out.txt. The redirection is attached to the command, not to the line. That detail matters later, because redirections are performed in the child process, after the fork.
Expansions run for each simple command
Before running either command, bash expands each word in the order described earlier in this module. The word "$HOME" is parameter-expanded, and because it is quoted, the result is not split. The word 'x y' is a single-quoted string, so no expansion happens and quote removal leaves x y. The word %s\n contains no expansion, but the backslash-n inside the single-quoted format argument is read by printf, not by bash. The result is a list of three arguments for printf: the format, the home directory, and the value with a space.
This is the point where a surprise would appear if a word were unquoted. An unquoted $HOME with a space in the directory name would become two arguments, and the printf output would change. Bash does the same work for every command, so the order of the steps is the same whether the command is a builtin or an external program.
The pipe and the processes
A pipeline needs more than one process. Bash creates a pipe with the pipe system call, which returns two file descriptors, one for reading and one for writing. Then it forks a child for each element of the pipeline. In the first child, the write end of the pipe becomes standard output, so the output of printf flows into the pipe. In the second child, the read end becomes standard input, so wc reads that output. Bash closes the spare ends in the parent and in both children, which is why the reader eventually sees end of file when the writer exits.
Each child then calls execve to replace itself with the target program. The printf command is a builtin, so bash runs it without an exec, although it is still in a child process because it is part of a pipeline. The wc command is an external program, so bash searches PATH and runs the binary. The file descriptors survive the exec, which is why the new program inherits the pipe and the redirections.
The redirection happens before the program starts
The redirection to out.txt is set up in the child that runs wc, before the exec. Bash opens the file with truncation, and it attaches the file to standard output. If the file could not be opened, bash would report the error and wc would never start. This is also why running a command with a redirection to a file that you are reading from can empty the file before the command reads it. The truncation happens first, and no program can undo it.
Waiting and the status
The parent bash does not exec anything for the pipeline. It waits for the children with the wait family of system calls. When the pipeline finishes, the exit status of the last command becomes the value of $?, and each element of the pipeline has its own status in PIPESTATUS. Only after that does bash print the next prompt. If you watch the process table during a long pipeline, you see the shell waiting as a parent, with the two children below it, which is the same picture of parent and child processes that appeared earlier in this course.
Tracing the whole sequence
You can watch these steps with strace, which records system calls as a program runs. Run it on a short command and look for the calls named in this section: pipe, the process creation call, execve, openat for the redirection, and the wait call in the parent. The output is long, but the order is the important part. The lesson in this module is not about memorizing system calls. It is about recognizing that every line you type goes through the same stages, so you can name the stage where a problem occurs.