A variable is a quantity whose value can vary; In essence, a variable name is a memory space for storing data;
The variables have local variables and environment variables;
A shell variable is a weakly typed variable that does not need to specify its variable type when declaring a variable, by default it is a character type, and does not need to follow the "first declaration and reuse" rule in C, whenever you want to use it;
Local variables:
is a variable that takes effect only in one shell, is not valid for the other shell, and disappears as the current shell ends;
The scope of local variables is limited to the shell in which they are declared;
In order for the child shell to inherit the variables of the current shell, it can be exported as an environment variable using the export built-in command;
Environment variables:
Also known as "global variables"; scope is the current shell process and its child processes;
(1) Export name=value when declaring multiple variables, separated by a space;
(2) Name=value
Export name
(3) Declare-x Name=value
(4) Name=value
Declare-x Name
View Environment variables: export,declare-x,printenv,env
Displays all defined variables and functions that have been loaded into the shell: set
$BASH: The full path of the BASH shell;
$BASH version of the _version:bash shell;
$LANG: The language environment of the current system;
$PWD: Current working directory;
$OLDPWD: previous working directory;
$PATH: The search path of the command;
$PS 1: Command prompt;
Variable naming: Variables in the shell must start with a letter or underscore, followed by numbers, letters, and underscores, with no limit to the length of the variable;
Variable naming laws:
1, can not use the reserved words in the program: such as If,else,then,while,for and so on;
2, can only use numbers, letters and underscores, and can not start with a number;
3. See the name and know the meaning;
4, the Uniform naming rule: Hump name law;
Variable assignment:
Name=value
Name= "Tom Jerry" #变量中有空格时, must be enclosed in quotation marks;
Name= "$name" #如果变量值引用的其它变量, need to enclose in double quotation marks, cannot use single quotation marks;
Variable Value:
You only need to precede the variable name with $;
$NAME
${name} #严谨一点的写法;
To cancel a variable:
The variable is freed from memory using the unset command followed by the variable name;
The function can be canceled, and unset can be followed by the function name to cancel the function;
Special variables:
The Shell has special-purpose read-only variables, whose values are determined only when the script is run;
Position parameters:
Called through the command line to the script in the script code;
$, $, ... : Corresponds to 1th, 2nd ... Parameters
$: script name itself;
$$: The PID when the script executes;
$!: PID of the last background process, i.e. the PID after executing the previous command;
$#: The number of arguments passed to the script;
$*: All parameters passed to the script, all parameters are combined into a string;
[Email protected]: All parameters passed to the script, each parameter is a separate string;
Command return value:
$?: The return value of the previous command;
The normal exit command takes 0 as its return value, and any return value other than 0 indicates that the command is not executing properly;
Success: 0
Failed: 1-255
Read-only variables:
Also known as constants, read-only variables are required to be assigned at the time of declaration, and cannot be modified or revoked;
Declare-r Name
ReadOnly Name
#readonly ro=100
Escape:
The escape character in the shell is the backslash "\", so that the character after the escape character appears purely as a character, without explaining its special meaning;
Variable references:
Strong quote: '
Weak reference: ""
Command substitution (command invocation):
Assigns the standard output of a command to a variable;
Two different ways:
Anti-quote call: '
$ () call #这种方式仅在bash valid in shell;
Variable Indirect reference:
If the value of the first variable is the name of the second variable, referring to the value of the second variable from the first variable is called an indirect variable reference;
Variable1=variable2
Variable2=value
Two ways to indirectly reference a variable:
Eval tempvar=\$ $variable 1
Tempvar=${!variable1}
eval command:
Eval two scans the variable;
#V1 =pwd
#echo $V 1
Pwd
#eval $V 1
/root
This article is from "Bear Bears" blog, please be sure to keep this source http://keithtt.blog.51cto.com/3080111/1813927
Variables in shell programming