In the Linux system for day-to-day operations or scripting, variables are more familiar with, but what are the types of variables, what is the difference in the specific usage? This paper is to share with you:
One, bash variable type:
Local variables
Local variables
Environment variables
Position variable
Special variables (built-in)
Second, local variables:
Varname=value: Scopes are available for the entire bash process;
Variable Naming conventions:
1. Can only contain letters, numbers and underscores, and begins with letters and underscores
2. It is best not to have the same name as the environment variables
3. See the name of the known meaning
Third, local variables:
Local Varname=value: Scope is current code segment;
Four, environment variable:
The scope is the current shell process and its child processes and cannot affect its parent process;
Export Varname=value "Exporting", if the variable has been defined can only be used with variable name export varname, i.e.
1. Export Varname=value
2. Varname=value
Export VarName
The script starts a child shell environment variable when the command is executed:
Scripts that are automatically executed by the system (not command-line startup) need to customize the environment variables required;
V. Position variables:
$1,$2,$3, ...
Cat first.sh Test.txt Hello
$1:first.sh
$2:test.txt
$3:hello
Shift: Kick off the parameters
Shift N: Kick out n parameters and kick off a parameter by default
VI. SPECIAL variables:
$?: The return value of the previous command execution state:
There may be two kinds of return values for program execution:
1. Results of program implementation
2. Does the program state return (0-255)
0 to perform the correct
1-255 Execution Error (1,2,127 system reservation);
$#: Gets the total number of arguments in the current shell command line
$*: Get all the arguments for the current shell "$ $ ..., controlled by IFS
$@: All parameters of this program "$1″" $2″ "$3″" ... ", not under the IFS control
$ Gets the file name of the currently executing shell script
$n gets the nth parameter value of the currently executing shell script, n=1..9
$$ Gets the process number (PID) of the current shell
$! PID that executes the previous instruction