
Recall first from Parsing commands and builtins
- Spaces and tabs separate unquoted words, and unquoted operators can delimit words without spaces.
- A redirection target belongs to the command's redirection list.
- A standalone builtin can change the shell's own process state.
Find the executable
3 min readThe last lesson handled builtins. Now you load an external program from the file system by name, fork a child, exec it, and have the parent wait and report the exit status.
On an embedded Linux image, a command can work when you type its full pathname and fail when you type only its name. You can reproduce that situation on Ubuntu with /bin/ls. Give a Bash subshell a search path that contains only /dev/null, then compare ls with /bin/ls. The difference follows from the command search rules documented in bash(1). The parentheses limit the PATH change to the subshell.
(
PATH=/dev/null
ls -d /bin
printf 'bare name status=%s\n' "$?"
/bin/ls -d /bin
printf 'explicit path status=%s\n' "$?"
)
/dev/null is a device file, so it cannot supply an executable named ls inside it. In a standard Bash session on Ubuntu, the bare name fails with status 127. The explicit pathname runs successfully and gives status 0. In both commands, -d tells ls to report the named directory itself. The /bin at the end is an argument to ls. Only the first word changes between the two calls.
For an external command such as ls, Bash can consult an environment variable containing a colon-separated list of directories to search for commands. With PATH=/usr/local/bin:/bin, a fresh search considers /usr/local/bin/ls before /bin/ls. If both directories supply a runnable ls, directory order determines which file the bare name selects. Bash can remember paths from earlier searches, so this order describes a fresh lookup. On a typical Ubuntu 24.04 installation, /bin is a symbolic link to /usr/bin. /bin/ls still works as a pathname, and the link explains why two different directory names can lead to the same installed executable.
A slash anywhere in the command name tells Bash to use the given pathname. /bin/ls starts at the root directory, and ./ls refers to a file named ls in the current directory. A bare ls searches the current directory only if the search path includes it. This distinction matters when you put a test executable in your working directory. ./ls asks Bash for that exact file. A bare ls may select the installed utility.
The shell supplies the executable pathname and the command's arguments separately. In ls -d /bin, the pathname might resolve to /bin/ls, while the argument vector contains ls, -d, and /bin. The first element conventionally names the program as invoked. The remaining elements carry the options and operands. Keeping the executable pathname separate from the arguments lets the shell run the same file with different options without changing how it locates the file.
Fork and exec the child
3 min readConsider an interactive Bash session running the foreground command /bin/sleep 1. You expect your shell to accept another line after sleep finishes. For this command, the shell calls fork() to create a child process and keeps running its own shell code in the parent. The child initially runs a copy of the shell code and can see the resolved pathname and arguments that were prepared before the fork. The parent and child can be scheduled in either order.
Both processes continue from the fork() call, and the return value tells each one which code to run. The child receives zero. The parent receives the positive process ID, or PID, of the new child. If fork() fails, it returns -1 to the parent and creates no child. The parent can report the error immediately. After a successful fork, it saves the returned PID so it can wait for this particular child.
In the child, the shell calls execve() with the chosen pathname, an argument vector, and an environment vector. For /bin/sleep 1, the arguments tell the new program that it was invoked as /bin/sleep and should sleep for one second. Linux opens the executable through the file system, checks whether it can execute the file, and sets up the new program's address space. Execution proceeds in the new program with the child's existing PID. This is the same PID the parent got from fork(), so the parent can wait for it even though the code running under that PID has changed.
A successful execve() never returns to the shell code in the child. If the call returns -1, the child is still running that code and can inspect the error. For instance, an attempted execution can fail if the file lacks execute permission. The child reports the failure and terminates. A small launcher can use _exit() on this child error path. Returning to the interactive loop would leave the child trying to accept commands alongside its parent.
The order matters. If the interactive shell itself successfully called execve() for /bin/sleep, its program would be replaced in the original shell process. Once sleep exited, there would be no shell code left in that process to read another line. By forking first, the shell can let the child replace its own program and keep the parent available to resume the prompt.
Wait and report the result
3 min readThe parent can call waitpid(child_pid, &status, 0) to collect the foreground child's termination status. With an ordinary untraced child, this blocks if the child is still running. If the child has already exited, Linux retains its termination information so the parent can collect it. A successful wait for a terminated child returns its PID, writes the encoded status through &status, and reaps the child, allowing the kernel to release the remaining process record.
The integer written into status encodes how the child terminated. The parent checks WIFEXITED(status) before using WEXITSTATUS(status) to extract the normal exit code. If WIFSIGNALED(status) is true, WTERMSIG(status) gives the terminating signal number. Bash represents a command terminated by signal number N with status 128 + N. A launcher checks the return value of waitpid() before interpreting status. If the wait fails with EINTR, the launcher can retry the wait for the same PID.
Bash makes the status of the most recent foreground command available through $?. A command can produce output and fail, or produce no output and succeed. The external true and false utilities make that distinction easy to observe. Both are quiet when run with no arguments, and their exit codes differ by design. The absolute paths in this example select the external programs.
/bin/true
printf 'true status=%s\n' "$?"
/bin/false
printf 'false status=%s\n' "$?"
The first printf reports 0 and the second reports 1. Read $? immediately after the command you care about. Bash expands $? when preparing the arguments for printf, so the number passed to printf belongs to the preceding command. Once printf finishes, its own status becomes the most recent one. To use a status in several later commands, capture it immediately with status=$? and refer to $status.
A program's exit code describes the result reported by that program after it starts. The shell can also report a failure to start it. Bash uses 127 when a command cannot be found and 126 when it finds a command but cannot execute it. A running program can also choose either value as its own exit code. When diagnosing a failure, read the number alongside the command and any diagnostic from the shell. In the first example, the change to PATH explains the failed lookup. In this example, /bin/false starts successfully and supplies status 1 itself.
So far each foreground command has consisted of a single external program. The next lesson, Pipes, asks how the shell connects one program's output to another program's input. The child processes you traced here are the processes the shell will connect.
Key takeaways
- Bash searches PATH for an external command name that contains no slash.
- fork creates a child, and a successful exec replaces the child’s program while preserving its process ID.
- The parent shell waits for the foreground child and makes the command status available as $?.
- Bash reports status 127 for a command it cannot find and 126 for a command it finds but cannot execute.
Reflect
- Explain to a colleague, in two sentences: resolve a bare command name using an ordered search path.
- Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
- What surprised you when you ran the commands yourself?
On a real board
Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.
References and further reading
- Man pagebash(1): command search and exit status
- Man pagefork(2): create a child process
- Man pageexecve(2): execute a program
- Man pagewaitpid(2): wait for a child to change state
Official documentation first. Links checked when the lesson was published.