1+ < ?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> < html xmlns ="http://www.w3.org/1999/xhtml "> < head > < meta http-equiv ="Content-Type " content ="text/html; charset=UTF-8 " /> < title > 5.19. envsubst - substitutes environment variables in shell format strings</ title > < link rel ="stylesheet " type ="text/css " href ="..//docbook.css " /> < meta name ="generator " content ="DocBook XSL Stylesheets V1.79.1 " /> < meta name ="keywords " content ="shell,bash,zsh, awk,sed,curl,expect, grep,sort " /> < link rel ="home " href ="../index.html " title ="Netkiller Shell 手札 " /> < link rel ="up " href ="index.html " title ="第 5 章 Shell command " /> < link rel ="prev " href ="ch05s18.html " title ="5.18. 信息摘要 " /> < link rel ="next " href ="miscellaneous.html " title ="第 6 章 其他命令 " /> </ head > < body > < a xmlns ="" href ="//www.netkiller.cn/ "> Home</ a > |
3+ < a xmlns ="" href ="//netkiller.github.io/ "> 简体中文</ a > |
4+ < a xmlns ="" href ="http://netkiller.sourceforge.net/ "> 繁体中文</ a > |
5+ < a xmlns ="" href ="/journal/index.html "> 杂文</ a > |
6+ < a xmlns ="" href ="//www.netkiller.cn/home/donations.html "> 打赏(Donations)</ a > |
7+ < a xmlns ="" href ="https://github.com/netkiller "> Github</ a > |
8+ < a xmlns ="" href ="http://my.oschina.net/neochen/ "> OSChina 博客</ a > |
9+ < a xmlns ="" href ="https://cloud.tencent.com/developer/column/2078 "> 云社区</ a > |
10+ < a xmlns ="" href ="https://yq.aliyun.com/u/netkiller/ "> 云栖社区</ a > |
11+ < a xmlns ="" href ="https://www.facebook.com/bg7nyt "> Facebook</ a > |
12+ < a xmlns ="" href ="http://cn.linkedin.com/in/netkiller/ "> Linkedin</ a > |
13+ < a xmlns ="" href ="https://zhuanlan.zhihu.com/netkiller "> 知乎专栏</ a > |
14+ < a xmlns ="" href ="//www.netkiller.cn/home/video.html "> 视频教程</ a > |
15+ < a xmlns ="" href ="//www.netkiller.cn/home/about.html "> About</ a > < div class ="navheader "> < table width ="100% " summary ="Navigation header "> < tr > < th colspan ="3 " align ="center "> 5.19. envsubst - substitutes environment variables in shell format strings</ th > </ tr > < tr > < td width ="20% " align ="left "> < a accesskey ="p " href ="ch05s18.html "> 上一页</ a > </ td > < th width ="60% " align ="center "> 第 5 章 Shell command</ th > < td width ="20% " align ="right "> < a accesskey ="n " href ="miscellaneous.html "> 下一页</ a > </ td > </ tr > </ table > < hr /> </ div > < table xmlns =""> < tr > < td > < iframe src ="//ghbtns.com/github-btn.html?user=netkiller&repo=netkiller.github.io&type=watch&count=true&size=large " height ="30 " width ="170 " frameborder ="0 " scrolling ="0 " style ="width:170px; height: 30px; " allowTransparency ="true "> </ iframe > </ td > < td > < iframe src ="//ghbtns.com/github-btn.html?user=netkiller&repo=netkiller.github.io&type=fork&count=true&size=large " height ="30 " width ="170 " frameborder ="0 " scrolling ="0 " style ="width:170px; height: 30px; " allowTransparency ="true "> </ iframe > </ td > < td > < iframe src ="//ghbtns.com/github-btn.html?user=netkiller&type=follow&count=true&size=large " height ="30 " width ="240 " frameborder ="0 " scrolling ="0 " style ="width:240px; height: 30px; " allowTransparency ="true "> </ iframe > </ td > </ tr > </ table > < div class ="section "> < div class ="titlepage "> < div > < div > < h2 class ="title " style ="clear: both "> < a id ="idp187 "> </ a > 5.19. envsubst - substitutes environment variables in shell format strings</ h2 > </ div > < div > < h3 class ="subtitle "> 替代品在shell环境变量的格式字符串,类似模版替换操作</ h3 > </ div > </ div > </ div >
16+
17+
18+ < p > </ p >
19+ < pre class ="screen ">
20+
21+ [root@localhost tmp]# echo "welcome $HOME ${USER:=a8m}" | envsubst
22+ welcome /root root
23+
24+ </ pre >
25+ < pre class ="screen ">
26+
27+ [root@localhost tmp]# cat config.template
28+ HOME=${HOME}
29+ USER=${USER}
30+
31+ [root@localhost tmp]# envsubst < config.template > config.conf
32+
33+ [root@localhost tmp]# cat config.conf
34+ HOME=/root
35+ USER=root
36+
37+ </ pre >
38+ < p > 只替换 ${USER} 变量</ p >
39+ < pre class ="screen ">
40+
41+ [root@localhost tmp]# envsubst '${USER}' < config.template > config.conf
42+ [root@localhost tmp]# cat config.conf
43+ HOME=${HOME}
44+ USER=root
45+
46+ </ pre >
47+ < p > 模版变量</ p >
48+ < pre class ="screen ">
49+
50+ ${var} var值( 与 $var 相同)
51+ ${var-$DEFAULT} 如果未设置 var,则将表达式计算为 $DEFAULT
52+ ${var:-$DEFAULT} 如果未设置var或者为空,则将表达式计算为 $DEFAULT
53+ ${var=$DEFAULT} 如果未设置 var,则将表达式计算为 $DEFAULT
54+ ${var:=$DEFAULT} 如果未设置var或者为空,则将表达式计算为 $DEFAULT
55+ ${var+$OTHER} 如果为 var,则将表达式计算为 $OTHER,,否则为空字符串
56+ ${var:+$OTHER} 如果为 var,则将表达式计算为 $OTHER,,否则为空字符串
57+
58+ </ pre >
59+ </ div > < div xmlns ="" id ="disqus_thread "> </ div > < script xmlns ="">
60+
61+ var disqus_config = function ( ) {
62+ this . page . url = "http://www.netkiller.cn" ; // Replace PAGE_URL with your page's canonical URL variable
63+ this . page . identifier = 'netkiller' ; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
64+ } ;
65+
66+ ( function ( ) { // DON'T EDIT BELOW THIS LINE
67+ var d = document , s = d . createElement ( 'script' ) ;
68+ s . src = '//netkiller.disqus.com/embed.js' ;
69+ s . setAttribute ( 'data-timestamp' , + new Date ( ) ) ;
70+ ( d . head || d . body ) . appendChild ( s ) ;
71+ } ) ( ) ;
72+ </ script > < noscript xmlns =""> Please enable JavaScript to view the < a href ="https://disqus.com/?ref_noscript "> comments powered by Disqus.</ a > </ noscript > < br xmlns ="" /> < script xmlns ="" type ="text/javascript " id ="clustrmaps " src ="//cdn.clustrmaps.com/map_v2.js?u=r5HG&d=9mi5r_kkDC8uxG8HuY3p4-2qgeeVypAK9vMD-2P6BYM "> </ script > < div class ="navfooter "> < hr /> < table width ="100% " summary ="Navigation footer "> < tr > < td width ="40% " align ="left "> < a accesskey ="p " href ="ch05s18.html "> 上一页</ a > </ td > < td width ="20% " align ="center "> < a accesskey ="u " href ="index.html "> 上一级</ a > </ td > < td width ="40% " align ="right "> < a accesskey ="n " href ="miscellaneous.html "> 下一页</ a > </ td > </ tr > < tr > < td width ="40% " align ="left " valign ="top "> 5.18. 信息摘要 </ td > < td width ="20% " align ="center "> < a accesskey ="h " href ="../index.html "> 起始页</ a > </ td > < td width ="40% " align ="right " valign ="top "> 第 6 章 其他命令</ td > </ tr > </ table > </ div > < script xmlns ="">
73+ ( function ( i , s , o , g , r , a , m ) { i [ 'GoogleAnalyticsObject' ] = r ; i [ r ] = i [ r ] || function ( ) {
74+ ( i [ r ] . q = i [ r ] . q || [ ] ) . push ( arguments ) } , i [ r ] . l = 1 * new Date ( ) ; a = s . createElement ( o ) ,
75+ m = s . getElementsByTagName ( o ) [ 0 ] ; a . async = 1 ; a . src = g ; m . parentNode . insertBefore ( a , m )
76+ } ) ( window , document , 'script' , '//www.google-analytics.com/analytics.js' , 'ga' ) ;
77+
78+ ga ( 'create' , 'UA-11694057-1' , 'auto' ) ;
79+ ga ( 'send' , 'pageview' ) ;
80+
81+ </ script > < script xmlns ="" async ="async ">
82+ var _hmt = _hmt || [ ] ;
83+ ( function ( ) {
84+ var hm = document . createElement ( "script" ) ;
85+ hm . src = "https://hm.baidu.com/hm.js?93967759a51cda79e49bf4e34d0b0f2c" ;
86+ var s = document . getElementsByTagName ( "script" ) [ 0 ] ;
87+ s . parentNode . insertBefore ( hm , s ) ;
88+ } ) ( ) ;
89+ </ script > < script xmlns ="" async ="async ">
90+ ( function ( ) {
91+ var bp = document . createElement ( 'script' ) ;
92+ var curProtocol = window . location . protocol . split ( ':' ) [ 0 ] ;
93+ if ( curProtocol === 'https' ) {
94+ bp . src = 'https://zz.bdstatic.com/linksubmit/push.js' ;
95+ }
96+ else {
97+ bp . src = 'http://push.zhanzhang.baidu.com/push.js' ;
98+ }
99+ var s = document . getElementsByTagName ( "script" ) [ 0 ] ;
100+ s . parentNode . insertBefore ( bp , s ) ;
101+ } ) ( ) ;
102+ </ script > < script xmlns ="" type ="text/javascript " src ="/js/q.js " async ="async "> </ script > </ body > </ html >
0 commit comments