summaryrefslogtreecommitdiff
path: root/posts/6/index.html
blob: 6c2617d659245ee89e43889ce0254e27c52272ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <meta name="generator" content="Hugo 0.105.0">

  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="author" content="garhve" />
  <meta property="og:url" content="https://blog.garhve.com/posts/6/" />
  <link rel="canonical" href="https://blog.garhve.com/posts/6/" /><link rel="alternate" type="application/atom+xml" href="https://blog.garhve.comindex.xml" title="blog | garhve&#39;s hub">

  <script type="application/ld+json">
  {
      "@context" : "http://schema.org",
      "@type" : "BlogPosting",
      "mainEntityOfPage": {
           "@type": "WebPage",
           "@id": "https:\/\/blog.garhve.com"
      },
      "articleSection" : "posts",
      "name" : "Mess with Bash(2)",
      "headline" : "Mess with Bash(2)",
      "description" : "More info is in this tutorial\nall value expansions (ie. all syntax with a $ prefix) can only expand inside quoted arguments if the argument was double-quoted . Single quotes will turn the dollar-syntax into literal characters, causing bash to output the dollar rather than expand its value in-place!\n\u0026ldquo;Value expansions ($...) must always be double-quoted.\u0026rdquo;\nNever leave a value expansion unquoted.\nRedirection File Redirection 0 is standard input, 1 is standard output, 2 is standard error",
      "inLanguage" : "en-US",
      "author" : "garhve",
      "creator" : "garhve",
      "publisher": "garhve",
      "accountablePerson" : "garhve",
      "copyrightHolder" : "garhve",
      "copyrightYear" : "2022",
      "datePublished": "2022-09-29 09:43:02 \u002b0800 CST",
      "dateModified" : "2022-09-29 09:43:02 \u002b0800 CST",
      "url" : "https:\/\/blog.garhve.com\/posts\/6\/",
      "keywords" : [  ]
  }
</script>
<title>Mess with Bash(2)</title>
  <meta property="og:title" content="Mess with Bash(2)" />
  <meta property="og:type" content="article" />
  <meta property="og:description" content="More info is in this tutorial
all value expansions (ie. all syntax with a $ prefix) can only expand inside quoted arguments if the argument was double-quoted . Single quotes will turn the dollar-syntax into literal characters, causing bash to output the dollar rather than expand its value in-place!
&amp;ldquo;Value expansions ($...) must always be double-quoted.&amp;rdquo;
Never leave a value expansion unquoted.
Redirection File Redirection 0 is standard input, 1 is standard output, 2 is standard error" />
  <meta name="description" content="More info is in this tutorial
all value expansions (ie. all syntax with a $ prefix) can only expand inside quoted arguments if the argument was double-quoted . Single quotes will turn the dollar-syntax into literal characters, causing bash to output the dollar rather than expand its value in-place!
&amp;ldquo;Value expansions ($...) must always be double-quoted.&amp;rdquo;
Never leave a value expansion unquoted.
Redirection File Redirection 0 is standard input, 1 is standard output, 2 is standard error" />
  <meta property="og:locale" content="en-us" /><meta property="og:image" content="" />
  
  

  
    <style>body{font-family:bree serif,sans-serif;-webkit-font-smoothing:antialiased;margin:0 20px}article{max-width:800px;margin-left:auto;margin-right:auto}a{color:#000;text-decoration:none}a:hover{font-weight:600;text-decoration:underline}.post-ads{margin:50px 0}.markdown-body{font-size:18px;max-width:100%}.markdown-body a{text-decoration:underline;text-decoration-color:#000}.markdown-body blockquote{margin:0;padding:0 1em;color:#57606a;border-left:.25em solid #d0d7de}.markdown-body pre{padding:16px;overflow:auto;border-radius:10px}.markdown-body code{padding:.2em .4em;font-size:85%;background-color:#f6f8fa;border-radius:6px;font-family:fira code,monospace}.markdown-body pre>code{padding:0;font-size:80%;background-color:inherit;border:0;font-family:fira code,monospace}.Chinese .markdown-body{line-height:200%}.site-date-catalog{font-size:2rem}.header-title{font-size:2rem;font-weight:700;margin-top:32px;font-family:bungee shade,sans-serif}.header-title a{text-decoration:none}.header-subtitle{color:#666}.header-items{margin:10px 0}.header-item{margin:0 5px}.header-line{width:100%;border-width:2px;border-color:#482936;border-style:solid none none none}.lang-switch{font-weight:600}#posts-list{min-height:600px}.posts-line{font-size:1.2rem;margin:12px 0}.posts-categories{font-size:.8rem;margin:auto;text-align:center}.posts-category{padding:3px 0;border:#000 2px solid;border-radius:5px}.site-footer{margin-top:50px}.site-footer-item{margin-right:12px}.post-content img{max-width:100%;display:block;margin-right:auto;margin-top:12px}.post-header{margin-bottom:50px}.post-title{font-size:2rem;font-weight:600}.post-tags{display:inline;font-weight:600;padding:2px 5px;margin-right:6px;border:#000 2px solid;border-radius:5px}.post-date{font-weight:800;font-style:italic}.post-author{float:right;font-weight:600}.page-content{min-height:60%}.post-content{margin-bottom:50px}.post-content p{hyphens:auto;line-height:1.8;text-justify:ideographic;margin-bottom:1em}.related-content{border-width:3px;border-style:solid;border-color:#000;padding:0 10px;margin-bottom:50px;margin-top:100px}.related-content li{margin:5px 0}.taxonomy-term{font-size:3rem}.gallery-img{text-align:center}.gallery-img span{text-align:center}.gallery-img-desc{font-size:.8em;font-weight:800}#disqus_thread{position:relative}#disqus_thread:after{content:"";display:block;height:55px;width:100%;position:absolute;bottom:0;background:#fff}@media screen and (max-width:600px){.header-title,.header-subtitle,.header-items{text-align:center}.posts-line{font-size:16px}.markdown-body{font-size:16px}.post-title{font-size:2rem}.post-content p{letter-spacing:.05em}}@media screen and (max-width:48em){.posts-category{display:none}}</style>
  
  
    <style>.container,.container-fluid{margin-right:auto;margin-left:auto}.container-fluid{padding-right:2rem;padding-left:2rem}.row{box-sizing:border-box;display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-flex:0;-ms-flex:0 1 auto;flex:initial;-webkit-box-orient:horizontal;-webkit-box-direction:normal;-ms-flex-direction:row;flex-direction:row;-ms-flex-wrap:wrap;flex-wrap:wrap;margin-right:-.5rem;margin-left:-.5rem}.row.reverse{-webkit-box-orient:horizontal;-webkit-box-direction:reverse;-ms-flex-direction:row-reverse;flex-direction:row-reverse}.col.reverse{-webkit-box-orient:vertical;-webkit-box-direction:reverse;-ms-flex-direction:column-reverse;flex-direction:column-reverse}.col-xs,.col-xs-1,.col-xs-10,.col-xs-11,.col-xs-12,.col-xs-2,.col-xs-3,.col-xs-4,.col-xs-5,.col-xs-6,.col-xs-7,.col-xs-8,.col-xs-9,.col-xs-offset-0,.col-xs-offset-1,.col-xs-offset-10,.col-xs-offset-11,.col-xs-offset-12,.col-xs-offset-2,.col-xs-offset-3,.col-xs-offset-4,.col-xs-offset-5,.col-xs-offset-6,.col-xs-offset-7,.col-xs-offset-8,.col-xs-offset-9{box-sizing:border-box;-webkit-box-flex:0;-ms-flex:0 0 auto;flex:none;padding-right:.5rem;padding-left:.5rem}.col-xs{-webkit-box-flex:1;-ms-flex-positive:1;flex-grow:1;-ms-flex-preferred-size:0;flex-basis:0;max-width:100%}.col-xs-1{-ms-flex-preferred-size:8.33333333%;flex-basis:8.33333333%;max-width:8.33333333%}.col-xs-2{-ms-flex-preferred-size:16.66666667%;flex-basis:16.66666667%;max-width:16.66666667%}.col-xs-3{-ms-flex-preferred-size:25%;flex-basis:25%;max-width:25%}.col-xs-4{-ms-flex-preferred-size:33.33333333%;flex-basis:33.33333333%;max-width:33.33333333%}.col-xs-5{-ms-flex-preferred-size:41.66666667%;flex-basis:41.66666667%;max-width:41.66666667%}.col-xs-6{-ms-flex-preferred-size:50%;flex-basis:50%;max-width:50%}.col-xs-7{-ms-flex-preferred-size:58.33333333%;flex-basis:58.33333333%;max-width:58.33333333%}.col-xs-8{-ms-flex-preferred-size:66.66666667%;flex-basis:66.66666667%;max-width:66.66666667%}.col-xs-9{-ms-flex-preferred-size:75%;flex-basis:75%;max-width:75%}.col-xs-10{-ms-flex-preferred-size:83.33333333%;flex-basis:83.33333333%;max-width:83.33333333%}.col-xs-11{-ms-flex-preferred-size:91.66666667%;flex-basis:91.66666667%;max-width:91.66666667%}.col-xs-12{-ms-flex-preferred-size:100%;flex-basis:100%;max-width:100%}.col-xs-offset-0{margin-left:0}.col-xs-offset-1{margin-left:8.33333333%}.col-xs-offset-2{margin-left:16.66666667%}.col-xs-offset-3{margin-left:25%}.col-xs-offset-4{margin-left:33.33333333%}.col-xs-offset-5{margin-left:41.66666667%}.col-xs-offset-6{margin-left:50%}.col-xs-offset-7{margin-left:58.33333333%}.col-xs-offset-8{margin-left:66.66666667%}.col-xs-offset-9{margin-left:75%}.col-xs-offset-10{margin-left:83.33333333%}.col-xs-offset-11{margin-left:91.66666667%}.start-xs{-webkit-box-pack:start;-ms-flex-pack:start;justify-content:flex-start;text-align:start}.center-xs{-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;text-align:center}.end-xs{-webkit-box-pack:end;-ms-flex-pack:end;justify-content:flex-end;text-align:end}.top-xs{-webkit-box-align:start;-ms-flex-align:start;align-items:flex-start}.middle-xs{-webkit-box-align:center;-ms-flex-align:center;align-items:center}.bottom-xs{-webkit-box-align:end;-ms-flex-align:end;align-items:flex-end}.around-xs{-ms-flex-pack:distribute;justify-content:space-around}.between-xs{-webkit-box-pack:justify;-ms-flex-pack:justify;justify-content:space-between}.first-xs{-webkit-box-ordinal-group:0;-ms-flex-order:-1;order:-1}.last-xs{-webkit-box-ordinal-group:2;-ms-flex-order:1;order:1}@media only screen and (min-width:48em){.container{width:49rem}.col-sm,.col-sm-1,.col-sm-10,.col-sm-11,.col-sm-12,.col-sm-2,.col-sm-3,.col-sm-4,.col-sm-5,.col-sm-6,.col-sm-7,.col-sm-8,.col-sm-9,.col-sm-offset-0,.col-sm-offset-1,.col-sm-offset-10,.col-sm-offset-11,.col-sm-offset-12,.col-sm-offset-2,.col-sm-offset-3,.col-sm-offset-4,.col-sm-offset-5,.col-sm-offset-6,.col-sm-offset-7,.col-sm-offset-8,.col-sm-offset-9{box-sizing:border-box;-webkit-box-flex:0;-ms-flex:0 0 auto;flex:none;padding-right:.5rem;padding-left:.5rem}.col-sm{-webkit-box-flex:1;-ms-flex-positive:1;flex-grow:1;-ms-flex-preferred-size:0;flex-basis:0;max-width:100%}.col-sm-1{-ms-flex-preferred-size:8.33333333%;flex-basis:8.33333333%;max-width:8.33333333%}.col-sm-2{-ms-flex-preferred-size:16.66666667%;flex-basis:16.66666667%;max-width:16.66666667%}.col-sm-3{-ms-flex-preferred-size:25%;flex-basis:25%;max-width:25%}.col-sm-4{-ms-flex-preferred-size:33.33333333%;flex-basis:33.33333333%;max-width:33.33333333%}.col-sm-5{-ms-flex-preferred-size:41.66666667%;flex-basis:41.66666667%;max-width:41.66666667%}.col-sm-6{-ms-flex-preferred-size:50%;flex-basis:50%;max-width:50%}.col-sm-7{-ms-flex-preferred-size:58.33333333%;flex-basis:58.33333333%;max-width:58.33333333%}.col-sm-8{-ms-flex-preferred-size:66.66666667%;flex-basis:66.66666667%;max-width:66.66666667%}.col-sm-9{-ms-flex-preferred-size:75%;flex-basis:75%;max-width:75%}.col-sm-10{-ms-flex-preferred-size:83.33333333%;flex-basis:83.33333333%;max-width:83.33333333%}.col-sm-11{-ms-flex-preferred-size:91.66666667%;flex-basis:91.66666667%;max-width:91.66666667%}.col-sm-12{-ms-flex-preferred-size:100%;flex-basis:100%;max-width:100%}.col-sm-offset-0{margin-left:0}.col-sm-offset-1{margin-left:8.33333333%}.col-sm-offset-2{margin-left:16.66666667%}.col-sm-offset-3{margin-left:25%}.col-sm-offset-4{margin-left:33.33333333%}.col-sm-offset-5{margin-left:41.66666667%}.col-sm-offset-6{margin-left:50%}.col-sm-offset-7{margin-left:58.33333333%}.col-sm-offset-8{margin-left:66.66666667%}.col-sm-offset-9{margin-left:75%}.col-sm-offset-10{margin-left:83.33333333%}.col-sm-offset-11{margin-left:91.66666667%}.start-sm{-webkit-box-pack:start;-ms-flex-pack:start;justify-content:flex-start;text-align:start}.center-sm{-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;text-align:center}.end-sm{-webkit-box-pack:end;-ms-flex-pack:end;justify-content:flex-end;text-align:end}.top-sm{-webkit-box-align:start;-ms-flex-align:start;align-items:flex-start}.middle-sm{-webkit-box-align:center;-ms-flex-align:center;align-items:center}.bottom-sm{-webkit-box-align:end;-ms-flex-align:end;align-items:flex-end}.around-sm{-ms-flex-pack:distribute;justify-content:space-around}.between-sm{-webkit-box-pack:justify;-ms-flex-pack:justify;justify-content:space-between}.first-sm{-webkit-box-ordinal-group:0;-ms-flex-order:-1;order:-1}.last-sm{-webkit-box-ordinal-group:2;-ms-flex-order:1;order:1}}@media only screen and (min-width:64em){.container{width:65rem}.col-md,.col-md-1,.col-md-10,.col-md-11,.col-md-12,.col-md-2,.col-md-3,.col-md-4,.col-md-5,.col-md-6,.col-md-7,.col-md-8,.col-md-9,.col-md-offset-0,.col-md-offset-1,.col-md-offset-10,.col-md-offset-11,.col-md-offset-12,.col-md-offset-2,.col-md-offset-3,.col-md-offset-4,.col-md-offset-5,.col-md-offset-6,.col-md-offset-7,.col-md-offset-8,.col-md-offset-9{box-sizing:border-box;-webkit-box-flex:0;-ms-flex:0 0 auto;flex:none;padding-right:.5rem;padding-left:.5rem}.col-md{-webkit-box-flex:1;-ms-flex-positive:1;flex-grow:1;-ms-flex-preferred-size:0;flex-basis:0;max-width:100%}.col-md-1{-ms-flex-preferred-size:8.33333333%;flex-basis:8.33333333%;max-width:8.33333333%}.col-md-2{-ms-flex-preferred-size:16.66666667%;flex-basis:16.66666667%;max-width:16.66666667%}.col-md-3{-ms-flex-preferred-size:25%;flex-basis:25%;max-width:25%}.col-md-4{-ms-flex-preferred-size:33.33333333%;flex-basis:33.33333333%;max-width:33.33333333%}.col-md-5{-ms-flex-preferred-size:41.66666667%;flex-basis:41.66666667%;max-width:41.66666667%}.col-md-6{-ms-flex-preferred-size:50%;flex-basis:50%;max-width:50%}.col-md-7{-ms-flex-preferred-size:58.33333333%;flex-basis:58.33333333%;max-width:58.33333333%}.col-md-8{-ms-flex-preferred-size:66.66666667%;flex-basis:66.66666667%;max-width:66.66666667%}.col-md-9{-ms-flex-preferred-size:75%;flex-basis:75%;max-width:75%}.col-md-10{-ms-flex-preferred-size:83.33333333%;flex-basis:83.33333333%;max-width:83.33333333%}.col-md-11{-ms-flex-preferred-size:91.66666667%;flex-basis:91.66666667%;max-width:91.66666667%}.col-md-12{-ms-flex-preferred-size:100%;flex-basis:100%;max-width:100%}.col-md-offset-0{margin-left:0}.col-md-offset-1{margin-left:8.33333333%}.col-md-offset-2{margin-left:16.66666667%}.col-md-offset-3{margin-left:25%}.col-md-offset-4{margin-left:33.33333333%}.col-md-offset-5{margin-left:41.66666667%}.col-md-offset-6{margin-left:50%}.col-md-offset-7{margin-left:58.33333333%}.col-md-offset-8{margin-left:66.66666667%}.col-md-offset-9{margin-left:75%}.col-md-offset-10{margin-left:83.33333333%}.col-md-offset-11{margin-left:91.66666667%}.start-md{-webkit-box-pack:start;-ms-flex-pack:start;justify-content:flex-start;text-align:start}.center-md{-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;text-align:center}.end-md{-webkit-box-pack:end;-ms-flex-pack:end;justify-content:flex-end;text-align:end}.top-md{-webkit-box-align:start;-ms-flex-align:start;align-items:flex-start}.middle-md{-webkit-box-align:center;-ms-flex-align:center;align-items:center}.bottom-md{-webkit-box-align:end;-ms-flex-align:end;align-items:flex-end}.around-md{-ms-flex-pack:distribute;justify-content:space-around}.between-md{-webkit-box-pack:justify;-ms-flex-pack:justify;justify-content:space-between}.first-md{-webkit-box-ordinal-group:0;-ms-flex-order:-1;order:-1}.last-md{-webkit-box-ordinal-group:2;-ms-flex-order:1;order:1}}@media only screen and (min-width:75em){.container{width:76rem}.col-lg,.col-lg-1,.col-lg-10,.col-lg-11,.col-lg-12,.col-lg-2,.col-lg-3,.col-lg-4,.col-lg-5,.col-lg-6,.col-lg-7,.col-lg-8,.col-lg-9,.col-lg-offset-0,.col-lg-offset-1,.col-lg-offset-10,.col-lg-offset-11,.col-lg-offset-12,.col-lg-offset-2,.col-lg-offset-3,.col-lg-offset-4,.col-lg-offset-5,.col-lg-offset-6,.col-lg-offset-7,.col-lg-offset-8,.col-lg-offset-9{box-sizing:border-box;-webkit-box-flex:0;-ms-flex:0 0 auto;flex:none;padding-right:.5rem;padding-left:.5rem}.col-lg{-webkit-box-flex:1;-ms-flex-positive:1;flex-grow:1;-ms-flex-preferred-size:0;flex-basis:0;max-width:100%}.col-lg-1{-ms-flex-preferred-size:8.33333333%;flex-basis:8.33333333%;max-width:8.33333333%}.col-lg-2{-ms-flex-preferred-size:16.66666667%;flex-basis:16.66666667%;max-width:16.66666667%}.col-lg-3{-ms-flex-preferred-size:25%;flex-basis:25%;max-width:25%}.col-lg-4{-ms-flex-preferred-size:33.33333333%;flex-basis:33.33333333%;max-width:33.33333333%}.col-lg-5{-ms-flex-preferred-size:41.66666667%;flex-basis:41.66666667%;max-width:41.66666667%}.col-lg-6{-ms-flex-preferred-size:50%;flex-basis:50%;max-width:50%}.col-lg-7{-ms-flex-preferred-size:58.33333333%;flex-basis:58.33333333%;max-width:58.33333333%}.col-lg-8{-ms-flex-preferred-size:66.66666667%;flex-basis:66.66666667%;max-width:66.66666667%}.col-lg-9{-ms-flex-preferred-size:75%;flex-basis:75%;max-width:75%}.col-lg-10{-ms-flex-preferred-size:83.33333333%;flex-basis:83.33333333%;max-width:83.33333333%}.col-lg-11{-ms-flex-preferred-size:91.66666667%;flex-basis:91.66666667%;max-width:91.66666667%}.col-lg-12{-ms-flex-preferred-size:100%;flex-basis:100%;max-width:100%}.col-lg-offset-0{margin-left:0}.col-lg-offset-1{margin-left:8.33333333%}.col-lg-offset-2{margin-left:16.66666667%}.col-lg-offset-3{margin-left:25%}.col-lg-offset-4{margin-left:33.33333333%}.col-lg-offset-5{margin-left:41.66666667%}.col-lg-offset-6{margin-left:50%}.col-lg-offset-7{margin-left:58.33333333%}.col-lg-offset-8{margin-left:66.66666667%}.col-lg-offset-9{margin-left:75%}.col-lg-offset-10{margin-left:83.33333333%}.col-lg-offset-11{margin-left:91.66666667%}.start-lg{-webkit-box-pack:start;-ms-flex-pack:start;justify-content:flex-start;text-align:start}.center-lg{-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;text-align:center}.end-lg{-webkit-box-pack:end;-ms-flex-pack:end;justify-content:flex-end;text-align:end}.top-lg{-webkit-box-align:start;-ms-flex-align:start;align-items:flex-start}.middle-lg{-webkit-box-align:center;-ms-flex-align:center;align-items:center}.bottom-lg{-webkit-box-align:end;-ms-flex-align:end;align-items:flex-end}.around-lg{-ms-flex-pack:distribute;justify-content:space-around}.between-lg{-webkit-box-pack:justify;-ms-flex-pack:justify;justify-content:space-between}.first-lg{-webkit-box-ordinal-group:0;-ms-flex-order:-1;order:-1}.last-lg{-webkit-box-ordinal-group:2;-ms-flex-order:1;order:1}}</style>
  

  

  <link href="/index.xml" rel="alternate" type="application/rss+xml"
    title="blog | garhve&#39;s hub">
  
  <link rel="preconnect" href="https://fonts.gstatic.com">
  <link href="https://fonts.googleapis.com/css?family=Bree+Serif|Bungee+Shade" rel="stylesheet">
  
  
  
</head>


<body>
  <article class="post English" id="article">
    <div class="row">
      <div class="col-xs-12">
        <div class="site-header">
          
<header>
  <div class="header-title">
    <a href="/"
      >Garhve</a
    >
  </div>
  <div class="header-subtitle"></div>
</header>
<div class="row end-md center-xs header-items">
</div>
<div class="row end-xs">
   
  <div class="lang-switch col-xs-3 col-xs-offset-9">
    <a href="/cn/">Chinese</a>
  </div>
   
</div>
<div class="header-line"></div>

        </div>
        <header class="post-header">
          <h1 class="post-title">Mess with Bash(2)</h1>
          
          <div class="row post-desc">
            <div class="col-xs-6">
              
              <time class="post-date" datetime="2022-09-29 09:43:02 CST">
                29 Sep 2022
              </time>
              
            </div>
            <div class="col-xs-6">
              
              <div class="post-author">
                <a target="_blank" href="https://garhve.com">@garhve</a>
              </div>
              
            </div>
          </div>
          
        </header>

        <div class="post-content markdown-body">
          
          <blockquote>
<p>More info is in this <a href="https://https://guide.bash.academy">tutorial</a></p>
</blockquote>
<blockquote>
<p>all value expansions (ie. all syntax with a <code>$</code> prefix) can only expand inside quoted arguments if the argument was  <em>double-quoted</em> . Single quotes will turn the dollar-syntax into literal characters, causing bash to output the dollar rather than expand its value in-place!</p>
</blockquote>
<p><em>&ldquo;Value expansions (<code>$...</code>) must <strong>always</strong> be double-quoted.&rdquo;</em></p>
<p><em>Never leave a value expansion unquoted.</em></p>
<h2 id="redirection">Redirection</h2>
<h3 id="file-redirection">File Redirection</h3>
<p><strong>0 is standard input, 1 is standard output, 2 is standard error</strong></p>
<p><code>[x] &gt; file, [x] &lt; file</code></p>
<p>Make <em><strong>File descriptor</strong></em>(FD) <code>x</code> write to / read from file.</p>
<blockquote>
<p><code>echo hello &gt; ~/World</code></p>
<p><code>read line &lt; ~/Word</code></p>
<p><code>rm file 2&gt;/dev/null</code></p>
</blockquote>
<h3 id="file-descriptor-copying">File Descriptor copying</h3>
<p><code>[x] &gt;&amp; y, [x] &lt;&amp; y</code></p>
<p>make FD <code>x</code> write to / read from FD <code>y</code>&rsquo;s stream</p>
<blockquote>
<p>the connection to the stream used by FD <code>y</code> is copied to FD <code>x</code></p>
<p><code>curl cip.cc &gt; result 2&gt;&amp;1</code></p>
<p><code>ping localhost &gt; result 2&gt;&amp;1</code></p>
<div class="highlight"><pre tabindex="0" style="background-color:#fff;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="font-style:italic"># exec can be used to change the file descriptors of bash itself,</span>
</span></span><span style="display:flex;"><span><span style="font-style:italic"># and if you use an x that doesn&#39;t yet exist,</span>
</span></span><span style="display:flex;"><span><span style="font-style:italic"># bash will create a new file descriptor (&#34;plug&#34;) for you with that number.</span>
</span></span><span style="display:flex;"><span><span style="font-style:italic"># - in command is to close new FD 3 we&#39;d created before.</span>
</span></span><span style="display:flex;"><span><span style="font-style:italic"># &gt;&amp;- is to close FD 1, &lt;&amp;- is to close FD0</span>
</span></span><span style="display:flex;"><span>exec 3&gt;&amp;1 &gt;mylog; echo moo; exec 1&gt;&amp;3 3&gt;&amp;-
</span></span></code></pre></div></blockquote>
<h3 id="appending-file-redirection">Appending file redirection</h3>
<p><code>[x] &gt;&gt; file</code></p>
<p>make FD <code>x</code> append to end of the file</p>
<blockquote>
<p>A stream to <code>file</code> is opened for writing in append mode and is connected to file descriptor <code>x</code>. The regular file redirection operator <code>&gt;</code> empties the file&rsquo;s contents when it opens the file so that only your bytes will be in the file.</p>
<p><code>echo hello &gt;&gt; ~/world</code></p>
<p><code>echo world &gt;&gt; ~/world</code></p>
</blockquote>
<h3 id="redirecting-standard-output-and-standard-error">Redirecting standard output and standard error</h3>
<p><code>&amp;&gt;file</code></p>
<p>Make both FD 1 (standard output) and FD 2 (standard error) write to file</p>
<blockquote>
<p>This is a convenience operator which does the same thing as <code>&gt;file 2&gt;&amp;1</code> but is more concise. Again, you can append rather than truncate by doubling the arrow: <code>&amp;&gt;&gt;file</code></p>
<p><code>ping localhost &amp;&gt;result</code></p>
</blockquote>
<h3 id="here-documents">Here documents</h3>
<div class="highlight"><pre tabindex="0" style="background-color:#fff;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="font-style:italic">&lt;&lt;delimiter
</span></span></span><span style="display:flex;"><span><span style="font-style:italic">	Here document
</span></span></span><span style="display:flex;"><span><span style="font-style:italic">delimiter</span>
</span></span></code></pre></div><p>Make FD 0 read from the string between <code>delimiter</code>s</p>
<blockquote>
<p>Here-Documents are great for reading blocks of text to command line.</p>
<div class="highlight"><pre tabindex="0" style="background-color:#fff;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>cat <span style="font-style:italic">&lt;&lt; EOF
</span></span></span><span style="display:flex;"><span><span style="font-style:italic">this is within here document
</span></span></span><span style="display:flex;"><span><span style="font-style:italic">I can write as many lines as I like
</span></span></span><span style="display:flex;"><span><span style="font-style:italic">and terminate with line of demiliter only
</span></span></span><span style="display:flex;"><span><span style="font-style:italic">EOF</span>	//end of heredoc
</span></span></code></pre></div></blockquote>
<h3 id="here-strings">Here strings</h3>
<p><code>&lt;&lt;&lt; string</code></p>
<p>Make FD 0 read from the <code>string</code></p>
<blockquote>
<p>Here strings are very similar to here documents but more concise. They are generally preferred over here documents.</p>
<div class="highlight"><pre tabindex="0" style="background-color:#fff;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>cat &lt;&lt;&lt; <span style="font-style:italic">&#34;This,
</span></span></span><span style="display:flex;"><span><span style="font-style:italic">is the here strings. tab will also be read.&#34;</span>
</span></span></code></pre></div></blockquote>
<h3 id="moving-file-decipher">Moving file decipher</h3>
<p><code>[x]&gt;&amp;y-, [x]&lt;&amp;y-</code></p>
<p>Replace FD <code>x</code> with FD <code>y</code> and close FD <code>y</code></p>
<blockquote>
<p>Easy way of <code>[x]&gt;&amp;y, y&gt;&amp;-</code></p>
<div class="highlight"><pre tabindex="0" style="background-color:#fff;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="font-style:italic"># 3&gt;&amp;1-: copy FD 1 to FD 3 and close FD 1.</span>
</span></span><span style="display:flex;"><span><span style="font-style:italic">#  &gt;&amp;3-: copy FD 3 to FD 1 and close FD 3.</span>
</span></span><span style="display:flex;"><span>exec 3&gt;&amp;1- &gt;mylog; echo moo; exec &gt;&amp;3-
</span></span></code></pre></div></blockquote>
<h3 id="reading-and-writing-with-file-descriptor">Reading and writing with file descriptor</h3>
<p><code>[x] &lt;&gt; file</code></p>
<p>Open FD <code>x</code> for both reading and writing to <code>file</code></p>
<blockquote>
<p>The file descriptor at x is opened with a stream to the file that can be used for writing as well as reading bytes. Usually you&rsquo;ll use two file descriptors for this. <strong>One of the rare cases where this is useful is when setting up a stream with a read/write device such as a network socket</strong>.</p>
<div class="highlight"><pre tabindex="0" style="background-color:#fff;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>exec 5&lt;&gt;aFile
</span></span><span style="display:flex;"><span>cat &gt;&amp;5 <span style="font-style:italic">&#34;Hello world&#34;</span>	<span style="font-style:italic"># make FD 1 write to where FD 5 currently writing, copy file descriptor FD 5 to FD 1</span>
</span></span><span style="display:flex;"><span>cat &lt;&amp;5			<span style="font-style:italic"># make FD 0 read from where FD 5 currently reading, copy file descriptor FD 5 to FD 0, then cat will send content to FD 1</span>
</span></span></code></pre></div></blockquote>
<p>Exercise</p>
<blockquote>
<p>Q: fix <code>exec 3&gt;&amp;2 2&gt;log; echo 'Hello!'; exec 2&gt;&amp;3</code> so that the message is properly saved into the <code>log</code> file and such that FD 3 is properly closed afterwards:</p>
<p>A:</p>
<ol>
<li><code>exec 3&gt;&amp;1- 3&gt;log; echo 'Hello!'; exec 1&gt;&amp;3-</code></li>
<li><code>exec 3&gt;&amp;1 1&gt;&amp;- 3&gt;log; echo 'Hello!'; exec 1&gt;&amp;3 3&gt;&amp;-</code></li>
</ol>
</blockquote>
<h2 id="expansion">Expansion</h2>
<h3 id="pathname-expansion">pathname expansion</h3>
<ol>
<li>
<p>pattern expansion is performed by bash before command even execute</p>
<blockquote>
<p><code>file *</code> will show info about all file in current directory. <code>*</code> will expand to content before <code>file</code> execute.</p>
</blockquote>
</li>
<li>
<p>A <em>glob</em> is the name of the type of pattern supported by the bash shell.</p>
<blockquote>
<p>basic glob name supported by bash<img src="https://assets.garhve.com/pictures/screenshots/2022/10/1665123982.png" alt="explanation"></p>
<p><img src="https://assets.garhve.com/pictures/screenshots/2022/10/1665133964.png" alt="example"></p>
<p><strong>Those glob will only affect current directory, explicit expression is required to working on other directory.</strong> <code>ls /sub/*</code></p>
</blockquote>
</li>
<li>
<p>extended glob can be enable to get more powerful but also easy confusing feature of bash</p>
<blockquote>
<p>bash: <code>shopt -s extglob</code>
zsh. : <code>setopt extendedglob</code>
<img src="https://assets.garhve.com/pictures/screenshots/2022/10/1665134156.png" alt="explanation"></p>
<p><img src="https://assets.garhve.com/pictures/screenshots/2022/10/1665134205.png" alt="!(my)* get expand because of * is outside !(), which makes it expland another whole pathname"></p>
</blockquote>
</li>
</ol>
<h3 id="command-substitution">Command Substitution</h3>
<p>we can expansion commands within commands, but must use double-quote <code>&quot;&quot;</code> instead of <code>''</code></p>
<div class="highlight"><pre tabindex="0" style="background-color:#fff;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="font-style:italic"># this will output contents in hello.h to screen</span>
</span></span><span style="display:flex;"><span>cat hello.h
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="font-style:italic"># this will expand `cat hello.h` to real contents in </span>
</span></span><span style="display:flex;"><span><span style="font-style:italic"># file hello.h and concatenate to previous sentence</span>
</span></span><span style="display:flex;"><span>echo <span style="font-style:italic">&#34;file hello.h contains contents of </span><span style="font-weight:bold">$(</span>cat hello.h<span style="font-weight:bold">)</span><span style="font-style:italic">&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="font-style:italic"># this will output &#39;file hello.h contains contents of $(cat hello.h)&#39;</span>
</span></span><span style="display:flex;"><span><span style="font-style:italic"># without expand command in $()</span>
</span></span><span style="display:flex;"><span>echo <span style="font-style:italic">&#39;file hello.h contains contents of $(cat hello.h)&#39;</span>
</span></span></code></pre></div><p>In command, <code>$()</code> is called <em>value expansion</em>, it consists of value-expansion prefix <code>$</code> and subshell <code>(...)</code>. A subshell is essentially a small new bash process that is used to run a command while the main bash shell waits for the result.</p>
<h2 id="parameters">Parameters</h2>
<p>There are three kind of parameters:</p>
<ol>
<li>Environment Parameter</li>
<li>Positional Parameter</li>
<li>Variables</li>
</ol>
<h3 id="environment-parameter">Environment Parameter</h3>
<p>environment variables exist at the process level. That means they are not a feature of the bash shell, but rather a feature of any program process on your system. They can inherit by children, but children&rsquo;s EV can&rsquo;t be given to parent.</p>
<h3 id="positional-parameter">Positional Parameter</h3>
<p>Just as name indicates, these kind of parameters indicate arguments&rsquo; position, and always starting from 0.</p>
<p>for example, imaging we have a script <code>rename</code>, arguments could be passed to it to extend its usage:</p>
<p><code>rename dir name</code> there, we passed <code>dir</code> and <code>name</code> as argument, so that positional parameters in script would be <code>$1</code> and <code>$2</code>, representing arguments respectively. after <code>$2</code>, such as <code>$3</code> is unset since there has no more argument.</p>
<blockquote>
<p>Positional Parameter is read-only</p>
</blockquote>
<p>a new usage: <code>bash -c 'ls &quot;$1&quot;' -- '/home'</code>. This will working like <code>ls /home</code>, dash is necessary since it is first variable in shell we ran commands and it makes positional value of arguments populated after it stand as we expect in shell single-quoted command gonna run in.</p>
<h3 id="special-parameter">Special Parameter</h3>
<p>Special parameters are parameters whose name is a single symbolic character, they are used to request certain state information from the bash shell. Like positional parameter, they are <em>read-only</em>.</p>
<p><img src="https://assets.garhve.com/pictures/screenshots/2022/10/1666054171.png" alt="different kinds of special parameters and the information they hold"></p>
<h3 id="variables">Variables</h3>
<p>definition: <code>name=value</code> <em>//no space around <code>=</code> like other programming language support</em></p>
<p>call: like <em>command expansion</em>, using variable is to expand it with prefix <code>$</code>, e.g. <code>$name</code></p>
<ul>
<li>Keep in mind, Expansion should always be double-quoted *</li>
</ul>
<h3 id="parameter-expansion">Parameter expansion</h3>
<blockquote>
<p><a href="https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html">GNU material</a></p>
</blockquote>
<p>we expand parameters by prefixing their name with a <code>$</code> symbol</p>
<blockquote>
<p>e.g. <code>name=me; echo hello &quot;$name&quot;</code>. <code>hello me</code>.</p>
</blockquote>
<p>In addition, we can put braces (<code>{</code> and <code>}</code>) around our parameter, which indicates where variable is about to begin and end.</p>
<blockquote>
<p>e.g. <code>name=orange; echo there are 4 &quot;${name}s&quot;</code> <code>there are 4 oranges</code>.</p>
<div class="highlight"><pre tabindex="0" style="background-color:#fff;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>name=orange
</span></span><span style="display:flex;"><span>echo <span style="font-style:italic">&#34;there are 4 </span><span style="font-weight:bold;font-style:italic">${</span>name<span style="font-weight:bold;font-style:italic">}</span><span style="font-style:italic">s.&#34;</span>	<span style="font-style:italic"># there are 4 oranges.</span>
</span></span><span style="display:flex;"><span>echo <span style="font-style:italic">&#34;there are 4 </span>$names<span style="font-style:italic">.&#34;</span>	<span style="font-style:italic"># there are 4 .</span>
</span></span></code></pre></div><p>here, we put <code>{}</code>around<code>name</code>so that bash can be told that suffix <code>s</code> is not a part of variable. otherwise, it will treat <code>names</code> as parameter and looking for its value, which is none in our example.</p>
</blockquote>
<p>parameter expansion brings up a powerful feature: <em>parameter expansion oerators</em></p>
<blockquote>
<p>While expanding a parameter, it is possible to apply an operator to the expanding value without alternate original value.</p>
<p>I use these mostly</p>
<div class="highlight"><pre tabindex="0" style="background-color:#fff;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="font-style:italic"># remove string before pattern   	${name#pattern} shortest	${name##pattern} longest</span>
</span></span><span style="display:flex;"><span><span style="font-style:italic"># remove string after pattern    	${name%pattern} longest 	${name%%pattern} shortest</span>
</span></span><span style="display:flex;"><span><span style="font-style:italic"># delete first matching pattern		${name/pattern}</span>
</span></span><span style="display:flex;"><span><span style="font-style:italic"># delete all matching pattern		${name//pattern}</span>
</span></span><span style="display:flex;"><span><span style="font-style:italic"># substitute pattern with string 	${name//pattern/string}</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>foo=<span style="font-style:italic">&#34;foo-bar-foobar&#34;</span>
</span></span><span style="display:flex;"><span>echo <span style="font-weight:bold;font-style:italic">${</span>foo#*-<span style="font-weight:bold;font-style:italic">}</span>		<span style="font-style:italic"># echoes &#39;bar-foobar&#39; (Removes &#39;foo-&#39; because that matches &#39;*-&#39;)</span>
</span></span><span style="display:flex;"><span>echo <span style="font-weight:bold;font-style:italic">${</span>foo##*-<span style="font-weight:bold;font-style:italic">}</span>		<span style="font-style:italic"># echoes &#39;foobar&#39; (Removes &#39;foo-bar-&#39;)</span>
</span></span><span style="display:flex;"><span>echo <span style="font-weight:bold;font-style:italic">${</span>foo%-*<span style="font-weight:bold;font-style:italic">}</span>		<span style="font-style:italic"># echoes &#39;foo-bar&#39;</span>
</span></span><span style="display:flex;"><span>echo <span style="font-weight:bold;font-style:italic">${</span>foo%%-*<span style="font-weight:bold;font-style:italic">}</span>		<span style="font-style:italic"># echoes &#39;foo&#39;</span>
</span></span><span style="display:flex;"><span>echo <span style="font-weight:bold;font-style:italic">${</span>foo/-<span style="font-weight:bold;font-style:italic">}</span>		<span style="font-style:italic"># echoes &#39;foobar-foobar&#39;</span>
</span></span><span style="display:flex;"><span>echo <span style="font-weight:bold;font-style:italic">${</span>foo//-<span style="font-weight:bold;font-style:italic">}</span>		<span style="font-style:italic"># echoes &#39;foobarfoobar&#39;</span>
</span></span><span style="display:flex;"><span>echo <span style="font-weight:bold;font-style:italic">${</span>foo//-/_<span style="font-weight:bold;font-style:italic">}</span>	<span style="font-style:italic"># echoes &#39;foo_bar_foobar&#39;</span>
</span></span></code></pre></div></blockquote>
<p><img src="https://assets.garhve.com/pictures/screenshots/2022/10/1665660923.png" alt="part of operators as shown picture"></p>

        </div>
        
          <div class="row">
            <div class="col-xs-12">
              
            </div>
          </div>

          
          <div style="height: 50px;"></div>
        

        <div class="site-footer">
  
  
  
</div>

      </div>
    </div>
  </article>

  

<script>
  
  
    
    
  
</script>

  

</body>

</html>