Skip to content

Commit 6b05c0c

Browse files
committed
docs: edit Here string
1 parent f748b82 commit 6b05c0c

2 files changed

Lines changed: 36 additions & 8 deletions

File tree

docs/quotation.md

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,14 @@ $ echo "$(cal)"
172172
Here 字符串是一种输入多行字符串的方法,格式如下。
173173
174174
```bash
175-
[command] << token
176-
177-
178-
179-
180-
175+
<< token
176+
text
181177
token
182178
```
183179
184-
它的格式分成开始标记和结束标记。开始标记是两个小于号 + Here 字符串的名称,名称可以随意取;结束标记是单独一行的 Here 字符串名称。两者之间就是多行字符串的内容。
180+
它的格式分成开始标记(`<< token`)和结束标记(`token`)。开始标记是两个小于号 + Here 字符串的名称,名称可以随意取;结束标记是单独一行的 Here 字符串名称。两者之间就是多行字符串的内容。
181+
182+
```bash
185183
186184
下面是一个输出 HTML 代码的例子。
187185
@@ -201,3 +199,31 @@ $ cat << _EOF_
201199
_EOF_
202200
```
203201
202+
Here 字符串内部会发生变量替换,但是双引号和单引号都失去语法作用,变成了普通字符。
203+
204+
```bash
205+
$ foo='hello world'
206+
$ echo << _example_
207+
$foo
208+
"$foo"
209+
'$foo'
210+
_example_
211+
212+
hello world
213+
"hello world"
214+
'hello world'
215+
```
216+
217+
上面例子中,变量`$foo`发生了替换,但是双引号和单引号都原样输出了,表明它们已经失去了引用的功能。
218+
219+
Here 字符串的本质是重定向,它将字符串重定向输出给某个命令,相当于包含了`echo`命令。所以,Here 字符串只适合那些可以接受标准输入作为参数的命令,对于其他命令无效,比如`echo`命令就不能用 Here 字符串。
220+
221+
```bash
222+
$ echo << _example_
223+
hello
224+
_example_
225+
```
226+
227+
上面例子不会有任何输出,因为 Here 字符串对于`echo`命令无效。
228+
229+
此外,Here 字符串也不能作为变量的值,只能用于命令的参数。

docs/script.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@ $ chmod +x script.sh
4949
# 给所有用户读权限和执行权限
5050
$ chmod +rx script.sh
5151
# 或者
52-
$ chmod 555 script.sh
52+
$ chmod 755 script.sh
5353

5454
# 只给脚本拥有者读权限和执行权限
5555
$ chmod u+rx script.sh
5656
```
5757

58+
脚本的权限通常设为`755`(拥有者有所有权限,其他人有读和执行权限)或者`700`(只有拥有者可以执行)。
59+
5860
除了执行权限,脚本调用时,一般需要指定脚本的路径。如果将脚本放在环境变量`$PATH`指定的目录中,就不需要指定路径了。因为 Bash 会自动到这些目录中,寻找是否存在同名的可执行文件。
5961

6062
建议在主目录新建一个`~/bin`子目录,专门存放可执行脚本,然后把`~/bin`加入`$PATH`

0 commit comments

Comments
 (0)