Skip to content

Commit 54967bb

Browse files
committed
docs(script): edit 参数终止符 --
1 parent b916b28 commit 54967bb

File tree

3 files changed

+50
-79
lines changed

3 files changed

+50
-79
lines changed

docs/script.md

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,16 +263,31 @@ shift "$(($OPTIND - 1))"
263263

264264
## 配置项参数终止符 `--`
265265

266-
变量当作命令的参数时,有时希望指定变量只能作为实体参数,不能当作配置项参数,这时可以使用配置项参数终止符`--`
266+
`-``--`开头的参数,会被 Bash 当作配置项解释。但是,有时它们不是配置项,而是实体参数的一部分,比如文件名叫做`-f``--file`
267267

268268
```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
271280
```
272281

273-
上面例子中,`--`强制变量`$myPath`只能当作实体参数(即路径名)解释。
282+
上面命令可以正确展示文件`-f``--file`的内容,因为它们放在`--`的后面,开头的`-``--`就不再当作配置项解释了。
283+
284+
如果要确保某个变量不会被当作配置项解释,就要在它前面放上参数终止符`--`
285+
286+
```bash
287+
$ ls -- $myPath
288+
```
274289

275-
如果变量不是路径名,就会报错。
290+
上面示例中,`--`强制变量`$myPath`只能当作实体参数(即路径名)解释。如果变量不是路径名,就会报错。
276291

277292
```bash
278293
$ myPath="-l"
@@ -282,13 +297,13 @@ ls: 无法访问'-l': 没有那个文件或目录
282297

283298
上面例子中,变量`myPath`的值为`-l`,不是路径。但是,`--`强制`$myPath`只能作为路径解释,导致报错“不存在该路径”。
284299

285-
参数终止符的作用主要是,如果破折号开头的参数要用作实体参数,就需要用到它
300+
下面是另一个实际的例子,如果想在文件里面搜索`--hello`,这时也要使用参数终止符`--`
286301

287302
```bash
288303
$ grep -- "--hello" example.txt
289304
```
290305

291-
上面命令在`example.txt`文件里面,搜索字符串`--hello`这个字符串是破折号开头,如果不用参数终止符,`grep`命令就会把`--hello`当作配置项参数,从而报错。
306+
上面命令在`example.txt`文件里面,搜索字符串`--hello`这个字符串是`--`开头,如果不用参数终止符,`grep`命令就会把`--hello`当作配置项参数,从而报错。
292307

293308
## exit 命令
294309

package-lock.json

Lines changed: 27 additions & 71 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
},
3434
"homepage": "https://github.com/wangdoc/bash-tutorial#readme",
3535
"dependencies": {
36-
"gh-pages": "^3.x",
36+
"gh-pages": "^3.2.1",
3737
"husky": "^4.3.8",
3838
"loppo": "^0.6.23",
3939
"loppo-theme-wangdoc": "^0.5.2"

0 commit comments

Comments
 (0)