질문 제목에 언급된 변수 확장 외에도 인라인 데이터를 ...로 읽을 때 데이터에 백틱이 포함된 경우 <<EOL
또 다른 걱정스러운 문제가 발생했습니다 .`, 오류가 발생합니다.
둘 다 대체 질문이지만 관련 옵션은 무엇이며 어디에 있습니까? ...(인라인 읽기가 다르게 동작하는 이유는 무엇입니까?)
이게 스크립트야
#!/bin/bash
echo '==$BASH' | \
while IFS= read -r line ; do echo "# output 1: $line" ;done
echo '==$BASH'>junk
while IFS= read -r line ; do echo "# output 2: $line" ;done <junk
while IFS= read -r line ; do echo "# output 3: $line" ;done <<EOL
==$BASH
EOL
while IFS= read -r line ; do echo "# output 4: $line" ;done <<EOL
`my backtick test
EOL
이것이 출력이다
# output 1: ==$BASH
# output 2: ==$BASH
# output 3: ==/bin/bash
...: bad substitution: no closing "`" in `my backtick test
답변1
쉘 확장은 Documentation Here의 주요 이점 중 하나입니다. 좋은 소식은 이 기능을 끌 수 있다는 것입니다.
노력하다:
while IFS= read -r line ; do echo "# output 3: $line" ;done <<'EOL'
==$BASH
EOL
while IFS= read -r line ; do echo "# output 4: $line" ;done <<'EOL'
`my backtick test
EOL
자세한 내용은 다음을 참조하세요.
> info bash
<...>
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.
The format of here-documents is:
<<[-]WORD
HERE-DOCUMENT
DELIMITER
No parameter expansion, command substitution, arithmetic expansion,
or filename expansion is performed on WORD. If any characters in WORD
are 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,
command substitution, and arithmetic expansion. In the latter case,
the character sequence `\newline' is ignored, and `\' must be used to
quote the characters `\', `$', and ``'.
If the redirection operator is `<<-', then all leading tab
characters are stripped from input lines and the line containing
DELIMITER. This allows here-documents within shell scripts to be
indented in a natural fashion.