File tree 3 files changed +50
-79
lines changed 3 files changed +50
-79
lines changed Original file line number Diff line number Diff line change @@ -263,16 +263,31 @@ shift "$(($OPTIND - 1))"
263
263
264
264
## 配置项参数终止符 ` -- `
265
265
266
- 变量当作命令的参数时,有时希望指定变量只能作为实体参数,不能当作配置项参数,这时可以使用配置项参数终止符 ` -- ` 。
266
+ ` - ` 和 ` -- ` 开头的参数,会被 Bash 当作配置项解释。但是,有时它们不是配置项,而是实体参数的一部分,比如文件名叫做 ` -f ` 或 ` --file ` 。
267
267
268
268
``` bash
269
- $ myPath=" ~/docs"
270
- $ ls -- $myPath
269
+ $ cat -f
270
+ $ cat --file
271
+ ```
272
+
273
+ 上面命令的原意是输出文件` -f ` 和` --file ` 的内容,但是会被 Bash 当作配置项解释。
274
+
275
+ 这是就可以使用配置项参数终止符` -- ` ,它的作用是告诉 Bash,在它后面的参数开头的` - ` 和` -- ` 不是配置项,只能当作实体参数解释。
276
+
277
+ ``` bash
278
+ $ cat -- -f
279
+ $ cat -- --file
271
280
```
272
281
273
- 上面例子中,` -- ` 强制变量` $myPath ` 只能当作实体参数(即路径名)解释。
282
+ 上面命令可以正确展示文件` -f ` 和` --file ` 的内容,因为它们放在` -- ` 的后面,开头的` - ` 和` -- ` 就不再当作配置项解释了。
283
+
284
+ 如果要确保某个变量不会被当作配置项解释,就要在它前面放上参数终止符` -- ` 。
285
+
286
+ ``` bash
287
+ $ ls -- $myPath
288
+ ```
274
289
275
- 如果变量不是路径名,就会报错。
290
+ 上面示例中, ` -- ` 强制变量 ` $myPath ` 只能当作实体参数(即路径名)解释。 如果变量不是路径名,就会报错。
276
291
277
292
``` bash
278
293
$ myPath=" -l"
@@ -282,13 +297,13 @@ ls: 无法访问'-l': 没有那个文件或目录
282
297
283
298
上面例子中,变量` myPath ` 的值为` -l ` ,不是路径。但是,` -- ` 强制` $myPath ` 只能作为路径解释,导致报错“不存在该路径”。
284
299
285
- 参数终止符的作用主要是,如果破折号开头的参数要用作实体参数,就需要用到它 。
300
+ 下面是另一个实际的例子,如果想在文件里面搜索 ` --hello ` ,这时也要使用参数终止符 ` -- ` 。
286
301
287
302
``` bash
288
303
$ grep -- " --hello" example.txt
289
304
```
290
305
291
- 上面命令在` example.txt ` 文件里面,搜索字符串` --hello ` 。这个字符串是破折号开头 ,如果不用参数终止符,` grep ` 命令就会把` --hello ` 当作配置项参数,从而报错。
306
+ 上面命令在` example.txt ` 文件里面,搜索字符串` --hello ` 。这个字符串是 ` -- ` 开头 ,如果不用参数终止符,` grep ` 命令就会把` --hello ` 当作配置项参数,从而报错。
292
307
293
308
## exit 命令
294
309
Original file line number Diff line number Diff line change 33
33
},
34
34
"homepage" : " https://github.com/wangdoc/bash-tutorial#readme" ,
35
35
"dependencies" : {
36
- "gh-pages" : " ^3.x " ,
36
+ "gh-pages" : " ^3.2.1 " ,
37
37
"husky" : " ^4.3.8" ,
38
38
"loppo" : " ^0.6.23" ,
39
39
"loppo-theme-wangdoc" : " ^0.5.2"
You can’t perform that action at this time.
0 commit comments