From a8ff6481390e2b43c8af520c4dcbe99743182ce1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Mon, 10 Feb 2025 17:30:12 +0100 Subject: [PATCH] notes: add executing commands section --- NOTES.md | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/NOTES.md b/NOTES.md index f632c57..d6aef6e 100644 --- a/NOTES.md +++ b/NOTES.md @@ -22,6 +22,8 @@ of filenames and commands and arguments. Performs any necessary _redirections_ and removes the **redirection operators** and their operands from the argument list. +Executes the command (see _Command Execution_); + TODO: add missing operations ### Quoting Rules @@ -367,8 +369,61 @@ bash-5.1$ echo "hello world" > $nonexist bash: $nonexist: ambiguous redirect ``` -## Subshell +### Executing Commands +cf. 3.7 Executing Commands +#### Simple Command Execution +cf. 3.7.1 Simple Command Expansion + +When a simple command is executed, the shell performs the following +expansions, assignments, and redirections, from left to right, in the +following order. + + 1. The words that the parser has marked as redirections are saved for later + processing. + + 2. The words that are not redirections are expanded (see _Shell Expansions_). + If any words remain after expansion, the first word is taken to be the name + of the command and the remaining words are the arguments. + + 3. Redirections are performed as described above (see _Redirections_). + +If no command name results, redirections are performed, but do not affect the +current shell environment. A redirection error causes the command to exit with +a non-zero status. + +If there is a command name left after expansion, execution proceeds as described +below. Otherwise, the command exits with a status of zero. + +##### Command Search and Execution +cf. 3.7.2 Command Search and Execution + +After a command has been split into words, if it results in a simple command and +an optional list of arguments, the following actions are taken. + + 1. The shell searches for it in the list of shell builtins. If a match is + found, that builtin is invoked. + + 2. If the name is not a builtin, and contains no slashes, Bash searches each + element of '$PATH' for a directory containing an executable file by that + name. If the search is unsuccessful, the shell prints an error message and + returns an exit status of 127. + + 3. If the search is successful, or if the command name contains one or more + slashes, the shell executes the named program in a separate execution + environment. Argument 0 is set to the name given, and the remaining + arguments to the command are set to the arguments supplied, if any. + + 4. If this execution fails because the file is not in executable format, and + the file is not a directory, it is assumed to be a "shell script" and the + shell executes it as described in _Shell Scripts_. + TODO: check if we need to implement the _Shell Scripts_ behaviour + + 5. The shell waits for the command to complete and collects its exit status. + +TODO remove $@ $* mentions as we don't have to implement that + +#### Subshell cf. 3.7.3 Command Execution Environment The shell has an execution environment, which consists of the following: