notes: add redirection section

Signed-off-by: Khaïs COLIN <kcolin@student.42lehavre.fr>
This commit is contained in:
Khaïs COLIN 2025-02-07 16:02:50 +01:00
parent 4e7e7b85a7
commit eff1eede66
No known key found for this signature in database

View file

@ -19,6 +19,11 @@ Parses the tokens into simple and compound commands (see *Shell Commands*)
Performs the various _Shell Expansions_, breaking the expanded tokens into lists Performs the various _Shell Expansions_, breaking the expanded tokens into lists
of filenames and commands and arguments. of filenames and commands and arguments.
Performs any necessary _redirections_ and removes the **redirection operators**
and their operands from the argument list.
TODO: add missing operations
### Quoting Rules ### Quoting Rules
cf. 3.1.2 Quoting cf. 3.1.2 Quoting
@ -252,7 +257,7 @@ by a slash in the pattern.
cf. 3.5.8.1 Pattern Matching cf. 3.5.8.1 Pattern Matching
Any character that appears in a pattern, other than the special pattern Any character that appears in a pattern, other than the special pattern
characters described below, matches itself. The NUL character may not occur in characters described below, matches itself. The NUL character may not occur in
a pattern. a pattern.
The special pattern characters have the following meanings: The special pattern characters have the following meanings:
@ -273,9 +278,83 @@ bash-5.1$ ls *'*'there
#### Quote Removal #### Quote Removal
cf. 3.5.9 Quote Removal cf. 3.5.9 Quote Removal
After the preceding expansions, all unquoted occurrences of the After the preceding expansions, all unquoted occurrences of the characters '''
characters ''' and '"' that did not result from one of the above and '"' that did not result from one of the above expansions are removed.
expansions are removed.
### Redirection
cf. 3.6 Redirections
Before a command is executed, its input and output may be "redirected" using a
special notation interpreted by the shell. "Redirection" allows commands' file
handles to be made to refer to different files, and can change the files the
command reads from and writes to.
The redirection operators may precede or appear anywhere within a simple command
or may follow a command.
e.g. this is the correct behaviour
```shell
bash-5.1$ ls > hello.txt *here
bash-5.1$ cat hello.txt
hello*there
hi*there
noonethere
```
Redirections are processed in the order they appear, from left to right.
e.g. this is the correct behaviour
```shell
bash-5.1$ ls > hello.txt share > here.txt *.txt
bash-5.1$ ls -l hello.txt here.txt
-rw-r--r-- 1 kcolin 2024_le-havre 0 Feb 7 15:54 hello.txt
-rw-r--r-- 1 kcolin 2024_le-havre 68 Feb 7 15:54 here.txt
bash-5.1$ cat here.txt
hello.txt
here.txt
log.txt
newlog-strict.txt
newlog.txt
share:
man
```
'<' refers to the standard input (fd 0, STDIN\_FILENO)
'>' refers to the standard output (fd 1, STDOUT\_FILENO)
TODO: check unless otherwise noted
The word following the redirection operator, unless otherwise noted, is
subjected to parameter expansion, filename expansion, word splitting, and
quote removal.
If it expands to more than one word, Bash reports an error, except when in posix
mode.
TODO: decide if we follow posix default or bash default
Note: This behaviour change is, as far as I can tell, not documented in the Bash
Reference Manual.
```shell
# bash behaviour
bash-5.1$ var="file1 file2"
bash-5.1$ echo "hello world" > $var
bash: $var: ambiguous redirect
# posix behaviour
bash-5.1$ var="file1 file2"
bash-5.1$ echo "hello world" > $var
bash-5.1$ cat "$var"
hello world
```
In bash mode and in posix mode, if the variable is not defined, bash prints the
following error:
```shell
bash-5.1$ echo "hello world" > $nonexist
bash: $nonexist: ambiguous redirect
```
## Subshell ## Subshell