Shell Basic
1: Variables and Parameters
1.1: variable substitude:
name of variable, value of variable; name is a placeholder for value; Referencing the its value is called variable substitude;
Let us carefully distinguish between thename of a variableand itsvalue. Ifvariable1 is the name of avariable, then$variable1 is a reference to itsvalue,the data item it contains.
Enclosing a referenced value indouble quotes (" ... ") does not interfere with variable substitution.
Using single quotes (' ... ') causes the variable name to be used literally, and no substitution will take place.
Note that $variable is actually a simplified form of${variable}
if [ -z "$uninitializedval" ]
then
echo "unitialized"
fi
1.2. Variable Assignment
= the assignment operator;
using "let" to assignment; let a=12+1
In "for" loop to assignment, for a in 1 2 3 4 do echo $a done
read value from stdin:
read a
1.3 Bash Variables Are Untyped
Bash does not segregateits variables by "type."
To lighten the burden of keeping track of variable types in a script, Bashdoes permit declaring variables
1.4. Special Variable Types
-
Local variables
-
Variables visible only within a code block or function (see also local variables in functions)
Environmental variables
Variables that affect the behavior of the shell and user interface.
Arguments passed to the script from the command line[1] :$0, $1, $2,$3 . . .
$0 is the name of the script itself,$1 is the first argument, $2 the second,$3 the third, and so forth. After $9, the arguments must be enclosed in brackets, for example,${10}, ${11}, ${12}.
The special variables $* and $@ denote all the positional parameters.
The shift command reassigns the positional parameters, in effect shifting them to the left one notch.
$1 <--- $2, $2 <--- $3, $3 <--- $4, etc.
The old $1 disappears, but $0 (the script name) does not change.