Here Documents 重定向的缩进问题
Here Documents 在 Shell 中就是一段带有特殊目的的代码段,它使用I/O重定向的形式将一个命令序列传递到一个交互程序或者命令中。具体不多解释了,符号是 << 。写脚本经常会用到,但很多时候由于代码的美观都会缩进来格式化代码,这样就会出问题了。
func_sayhello() {
cat << EOF > sayhello
echo "Hello sun!"
echo "Hello moon!"
EOF
}
看起来很美观很科学,可这是出错的!Here Documents默认是不支持前面有任何缩进的,只能写成这样:
func_sayhello() {
cat << EOF > sayhello
echo "Hello sun!"
echo "Hello moon!"
EOF
}
那有方法鱼与熊掌都要吗?答案是肯定的!使用 <<- 即可:
func_sayhello() {
cat <<- EOF > sayhello
echo "Hello sun!"
echo "Hello moon!"
EOF
}
<<- 的 -代表忽略前面所有 TAB ,这样就可以兼得了。这个小细节在日常写脚本的时候也是很重要的,最后附上 man 的说明:
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 stan- dard input for a command. The format of here-documents is: <<[-]word here-document delimiter No parameter 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. 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.
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 stan-
dard input for a command.
The format of here-documents is:
<<[-]word
here-document
delimiter
No parameter 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. 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.
Here Documents 在 Shell 中就是一段带有特殊目的的代码段,它使用I/O重定向的形式将一个命令序列传递到一个交互程序或者命令中。具体不多解释了,符号是 << 。写脚本经常会用到,但很多时候由于代码的美观都会缩进来格式化代码,这样就会出问题了。
func_sayhello() {
cat << EOF > sayhello
echo "Hello sun!"
echo "Hello moon!"
EOF
}
看起来很美观很科学,可这是出错的!Here Documents默认是不支持前面有任何缩进的,只能写成这样:
func_sayhello() {
cat << EOF > sayhello
echo "Hello sun!"
echo "Hello moon!"
EOF
}
那有方法鱼与熊掌都要吗?答案是肯定的!使用 <<- 即可:
func_sayhello() {
cat <<- EOF > sayhello
echo "Hello sun!"
echo "Hello moon!"
EOF
}
<<- 的 -代表忽略前面所有 TAB ,这样就可以兼得了。这个小细节在日常写脚本的时候也是很重要的,最后附上 man 的说明:
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 stan- dard input for a command. The format of here-documents is: <<[-]word here-document delimiter No parameter 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. 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.
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 stan-
dard input for a command.
The format of here-documents is:
<<[-]word
here-document
delimiter
No parameter 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. 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.