变量与函数:Bash 脚本编程指南
1. 变量的引用与引号使用
在编写 Bash 脚本时,变量的引用和引号的使用至关重要。单引号和双引号有着不同的作用,单引号会将其中的内容原样输出,不会进行变量扩展。例如:
#!/bin/bash
directory='scripting'
# does the directory exist?
echo '"Directory $directory is undetermined since we have no \
logic in this script"'
运行该脚本,输出为:
"Directory $directory is undetermined since we have no logic in
this script"
双引号则允许变量扩展,当你需要在字符串中使用变量时,通常会使用双引号。这是因为脚本中经常会遇到包含空格和变量的字符串,使用双引号可以在保留文本的同时扩展变量。
2. 变量操作
2.1 数组操作
在 Bash 中,数组是一种特殊的变量,它可以存储多个由空格分隔的字符串。定义数组的方式是使用括号并将字符串包含在其中,例如:
demo@cli1:~/variables$ TestArray=(first second third fourth)
超级会员免费看
订阅专栏 解锁全文
1892

被折叠的 条评论
为什么被折叠?



