notes: clarify here doc section and move it to the correct place

This commit is contained in:
Khaïs COLIN 2025-02-11 14:14:00 +01:00
parent 78298fbb1d
commit 0637a637b9
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo

125
NOTES.md
View file

@ -336,10 +336,9 @@ man
'>' 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.
The word following the redirection operator, unless the redirection operator is
'<<', is subjected to parameter expansion, filename expansion, word splitting,
and quote removal.
If it expands to more than one word, Bash reports an error.
@ -357,6 +356,65 @@ bash-5.1$ echo "hello world" > $nonexist
bash: $nonexist: ambiguous redirect
```
#### Here Documents
cf. Bash Reference Manual 3.6.6 Here Documents
This type of redirection instructs the shell to read input from the current
source until a line containing only word (with no trailing blanks) is seen. All
of the lines read up to that point are then used as the standard input for a
command.
No parameter and variable expansion, command substitution, arithmetic expansion,
or filename expansion is performed on word.
If any part of word is quoted, the delimiter is the result of quote removal on
word, and the lines in the here-document are not expanded. If word is unquoted,
all lines of the here-document are subjected to parameter expansion, and the
character sequence \\newline is ignored.
This is the correct behaviour for quoting and parameter expansion:
```shell
bash-5.2$ cat << EOF
> hello
> $$
> EOF
hello
1491742
bash-5.2$ cat << "EOF"
> hello
> $$
> EOF
hello
$$
bash-5.2$ cat << 'E'OF
> hello
> $$
> EOF
hello
$$
bash-5.2$ cat << $USER
> hello
> khais
> $USER
hello
khais
bash-5.2$ echo $USER
khais
bash-5.2$ cat << "$USER"
> $USER
```
TODO: do we need to ignore \\newline? subject says \\ is not required..
If we have to implement it, this is the correct behaviour:
```shell
bash-5.2$ cat << EOF
> hello \
world
> EOF
hello world
```
### Executing Commands
cf. 3.7 Executing Commands
@ -442,65 +500,6 @@ executed command consists of the shell's initial environment, whose values may
be modified in the shell, less any pairs removed by the 'unset' builtin, plus
any additions via the 'export' command.
## Here Documents
cf. Bash Reference Manual 3.6.6 Here Documents
This type of redirection instructs the shell to read input from the current
source until a line containing only word (with no trailing blanks) is seen. All
of the lines read up to that point are then used as the standard input for a
command.
No parameter and variable expansion, command substitution, arithmetic expansion,
or filename expansion is performed on word.
If any part of word is quoted, the delimiter is the result of quote removal on
word, and the lines in the here-document are not expanded. If word is unquoted,
all lines of the here-document are subjected to parameter expansion, and the
character sequence \\newline is ignored.
This is the correct behaviour for quoting and parameter expansion:
```shell
bash-5.2$ cat << EOF
> hello
> $$
> EOF
hello
1491742
bash-5.2$ cat << "EOF"
> hello
> $$
> EOF
hello
$$
bash-5.2$ cat << 'E'OF
> hello
> $$
> EOF
hello
$$
bash-5.2$ cat << $USER
> hello
> khais
> $USER
hello
khais
bash-5.2$ echo $USER
khais
bash-5.2$ cat << "$USER"
> $USER
```
TODO: do we need to ignore \\newline? subject says \\ is not required..
If we have to implement it, this is the correct behaviour:
```shell
bash-5.2$ cat << EOF
> hello \
world
> EOF
hello world
```
## Definitions
cf. [Bash Reference Manual](https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Definitions)
cf. 2 Definitions