我编写以下shell脚本:
#!/bin/sh
TEST_VAR="HELLO"
cat > test-1.txt << EOF
TEST_VAR is ${TEST_VAR}
EOF
cat > test-2.txt << "EOF"
TEST_VAR is ${TEST_VAR}
EOF
但是我发现结果是不同的:
test-1.txt
是 TEST_VAR is HELLO
和:
test-2.txt
是 TEST_VAR is ${TEST_VAR}
有人可以帮忙解释一下吗?
在“在下面的行导致了问题:
cat > test-2.txt << "EOF"
原因是在man bash中:引用时没有参数扩展。
Here Documents
This type of redirection instructs the shell to read input from the current source until a line containing only delimiter (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 and variable expansion, command substitution, arithmetic expansion, or pathname 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, 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.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句