Original: http://blog.51cto.com/woyaoxuelinux/1863045
shell: weakly typed programming language
Strong: variables must be declared before use, and even need to be initialized;
Weak: Variables are declared when they are used, even without distinguishing between types;
Variable assignment: VAR_NAME=VALUE
bash variable type:
environment variable
local variable (local variable)
position variable
special variable
local variable:
set VARNAME=VALUE: The scope is the entire bash process;
local variable:
local VARNAME=VALUE: the scope is the current code segment;
Environment variables: The scope is the current shell process and its subprocesses;
export VARNAME=VALUE
VARNAME=VALUE
export VARNAME
"Export"
position variable:
$0,$1, $2, ... $0 means the command itself, $1 means the first parameter, and so on $3 means the third parameter
shift n rotation, n represents the number to rotate the next nth parameter to the first parameter
eg: vim shift.sh
#!/bin/bash
#
echo $1
shift 2
echo $1
shift 2
echo $1
./shift.sh 1 2 3 4 5
[root@xuelinux test]# ./shift.sh 1 2 3 4 5
1
3
5
Special variables:
$?: The return value of the execution status of the previous command;
$#: number of parameters
$*: parameter list
$@: parameter list
Program execution, there may be two types of return values:
program execution result
Program status return code (0-255)
0: correct execution
1-255: Error execution, 1, 2, 127 system reservation;
Output redirection:
> output override redirection
> output append redirection
2> Error output override redirection
2>>Error output append redirection
&> correct error output redirection
Undo variables:
unset VARNAME VARNAME is the variable name
View variables in the current shell:
set VARNAME but set can be omitted VARNAME is the variable name
The command to view the environment variables in the current shell is as follows:
printenv
env
export
Script: The stacking of commands, according to actual needs, combined with the source program of the command flow control mechanism
shebang: magic number
#!/bin/bash
# comment line, do not execute
/dev/null: software device, bit bucket, data black hole
The script starts a subshell process when it is executed;
Scripts started on the command line inherit the current shell environment variables;
Scripts automatically executed by the system (non-command line startup) need to self-define each environment variable;