Bash脚本编程:变量、参数与条件逻辑全解析
1. 变量与引号的使用
在Bash脚本中,变量的赋值和使用需要特别注意引号的使用。例如,当我们尝试给变量 username 赋值为 Carol Smith 时,如果不使用引号,会出现错误:
#!/bin/bash
# This is our first comment. It is also good practice to comment all scripts.
username=Carol Smith
echo "Hello $username!"
运行这个脚本会得到错误信息:
$ ./new_script.sh
./new_script.sh: line 5: Smith: command not found
Hello !
这是因为Bash解释器逐行解释脚本,它将 username=Carol 视为给变量 username 赋值为 Carol ,而将空格后面的 Smith 视为一个命令。为了将空格和 Smith 都包含在变量的值中,我们需要使用双引号:
#!/bin/bash
# This is our first comment. It is also good practice to comm
超级会员免费看
订阅专栏 解锁全文
2284

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



