当前位置: 首页 > 工具软件 > bash-guide > 使用案例 >

读书笔记之 Advanced Bash-Scripting Guide Chapter 4 Introduction to Variables and Parameters

叶光华
2023-12-01
1. Note that $variable is actually a simplified form of ${variable}.
2. An uninitialized variable has no value, however it acts as if it were 0 in an arithmetic operation. This is undocumented (and probably non-portable) behavior, and should not be used in a script.(P31)
3. 此处a的值为21, 使用let对变量赋值时,会对等号后面的算术式进行算术运算,将运算后的值赋给变量
let a=16+5
echo "The value of \"a\" is now $a."
4. 注意代码中的注释。Variable assignment using the $(...) mechanism (a newer method than backquotes).
a=`ls -l` # Assigns result of 'ls -l' command to 'a'
echo $a   # Unquoted, however, it removes tabs and newlines.
echo
echo "$a" # The quoted variable preserves whitespace.
5. If a script sets environmental variables, they need to be "exported", that is, reported to the environment local to the script. This is the function of the export command. A script can export variables only to child processes.
6. Arguments passed to the script from the command line: $0, $1, $2, ..., ${10}, ${11},...  $0 is the name of the script itself. The special variables $* and $@ denote all the positional parameters. $# Number of args passed (不包括脚本本身).
7. The shift command reassigns the positional parameters, in effect shifting them to the left one notch. The old $1 disappears, but $0 (the script name) does not change.  The shift command works in a similar fashion on parameters passed to a function.
 类似资料: