Calvin's Tech Space

成于坚忍,毁于浮躁

   :: 首页 :: 联系 :: 聚合  :: 管理
一个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信息:
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
posted on 2011-05-26 18:12 calvin 阅读(377) 评论(0)  编辑  收藏 所属分类: Linux

只有注册用户登录后才能发表评论。


网站导航: