一個here document就是一段帶有特殊目的的代碼段. 它使用IO重定向的形式將一個命令序列傳遞到一個交互程序或者命令中, 比如frp,cat或者ex文本編輯器.
interactive-program <<delimiter
command #1
command #2
...
delimiter
默認情況下,變量會被替換:
?$ cat << EOF
?> Working dir $PWD
?> EOF
?Working dir /home/user
如果delimiter用雙引號引起來,則不會有變量替換:
?$ cat << "EOF"
?> Working dir $PWD
?> EOF
?Working dir $PWD
重定向 符號 << 可以使用 <<- ,在這種情況下,heredoc 文本中的前導 tab 字符會被刪除,但是空格不會被刪除!
注意:
結束標記前一定不能有任何空格或者tab,否則執行失敗!
heredoc 另外一個比較有用的是顯示消息塊,如Usage信息:
參考:
http://tldp.org/LDP/abs/html/here-docs.html
http://febird.iteye.com/blog/588509
http://en.wikipedia.org/wiki/Here_document
interactive-program <<delimiter
command #1
command #2
...
delimiter
默認情況下,變量會被替換:
?$ cat << EOF
?> Working dir $PWD
?> EOF
?Working dir /home/user
如果delimiter用雙引號引起來,則不會有變量替換:
?$ cat << "EOF"
?> Working dir $PWD
?> EOF
?Working dir $PWD
重定向 符號 << 可以使用 <<- ,在這種情況下,heredoc 文本中的前導 tab 字符會被刪除,但是空格不會被刪除!
注意:
結束標記前一定不能有任何空格或者tab,否則執行失敗!
heredoc 另外一個比較有用的是顯示消息塊,如Usage信息:
cat <<End-of-message ------------------------------------- This is line 1 of the message. This is line 2 of the message. This is line 3 of the message. This is line 4 of the message. This is the last line of the message. ------------------------------------- End-of-message
參考:
http://tldp.org/LDP/abs/html/here-docs.html
http://febird.iteye.com/blog/588509
http://en.wikipedia.org/wiki/Here_document