From 0637a637b9c6a1224feddab414bb3211afcaf083 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Tue, 11 Feb 2025 14:14:00 +0100 Subject: [PATCH] notes: clarify here doc section and move it to the correct place --- NOTES.md | 125 +++++++++++++++++++++++++++---------------------------- 1 file changed, 62 insertions(+), 63 deletions(-) diff --git a/NOTES.md b/NOTES.md index 3b654b8..be8fa39 100644 --- a/NOTES.md +++ b/NOTES.md @@ -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