Variable type: global variable (environment variable) and local variable (local variable)
Environment variables can be used in shells that define their shell and any child processes it derives from. Local variables can only be used in functions/scripts that define them. Some of the variables are created by the user, while others are proprietary shell variables. 1. Global variables (Environment variables):
Environment variables can be used to define the run-time environment of the shell, which can be defined and modified in the configuration file, or set on the command line, but the modification in the command line is lost when the terminal restarts, so it is best to modify the user's home directory in the configuration file. bash_profile "file or global Configuration"/etc/profile","/etc/bashrc"file, or"/etc/profile.d"file. The environment variables are placed in the profile file, and the values are initialized each time the user logs on. such as home, user, SHELL, UID and so on before the user login has been/bin/login program set up.
common system Environment variables:
tmout: set the error waiting time for automatic exit
HOSTTYPE: System File System type
histsize: history Command Record Bar count
Home : directory to enter when user logs in
UID: ID of current user
Shell: Current Shell interpreter
PWD: Current path (the value will change each time the directory changes)
path: executable file default path
Wait a minute.
You can use Echo to display view global variables (Eg:echo $HOME). Env (or printenv), set can also be used to view the system's environment variables, but not individually. and use unset to temporarily cancel the environment variable (eg:unset USER), to be permanently active or to write to a configuration file
Customizing environment Variables (using Export):
①export Variable name =value
② variable name =value;export variable name
③declare-x Variable name =value
This is still a temporary effect, after the shell terminal is closed and disappeared, write to the configuration file will be permanent (note: The script that needs to run once after writing to the configuration file can take effect, otherwise it will take effect when restart)
the three ways to test the command line are as follows:
For more detailed information on environment variables path and export, refer to: Linux environment Variables and system programming learning Notes 2, local variables (local variables):
Local variables are used in scripts in the user's current shell lifetime. When a variable is declared as Local in a function, the variable is a partial variable and is valid only in this function.
Definition:
Variable name =value
Variable name = ' value '
Variable name = "Value"
Variable name requirements in the shell: generally follow the letter, number, down line composition, can not start with a number
Eg: The following script executes (can be tested interactively and non-interactive) what output (C and C are equivalent to {C}).
a=192.168.1.1
b= ' 192.168.1.2 '
c= ' 192.168.1.3 '
echo ' a= $a '
echo ' b= $b '
echo ' c=${c} '
a= 192.168.1.1-$b
b= ' 192.168.1.2-$b '
c= ' 192.168.1.3-$b "
echo" a= $a "
echo" b= $b "
echo" C=${c} "
The output results are as follows:
a=192.168.1.1
b=192.168.1.2
c=192.168.1.3
a=192.168.1.1-192.168.1.2
b=192.168.1.2-$b
c=192.168.1.3-192.168.1.2-$b
Summary and Analysis:
The difference between single and double quotes is that if there is a variable in single quotation marks, the existence of the variable as a string will not be parsed, the output is as it is, and in double quotes if there is a variable, the variable is parsed out of its specific value and added to the string. ① can directly define content including numbers, strings, path names, and so on, suitable for defining numbers ② single quotes are suitable for pure definition strings, ③ and double quotes are suitable for the definition of content containing variables in the contents of a string. (Habits: Numbers and a simple string without spaces with quotes, other long, especially the string with a space plus double quotes; "$ variable name", but do not want to parse the single quotes, but generally appear $ is to resolve variables, so single quotes less used)
Note: the features of single and double quotes are not universal, as follows:
In the ordinary shell:
Ett=123
Echo ' $ETT ' //print $ett (single quotes not resolved)
echo "$ETT" //Print 123 (double quote resolution)
and invoke the shell variable in awk:
awk ' begin {print ' $ETT '} '//print 123 (single quote resolution)
awk ' begin {print ' $ETT '} '//print $ett (double quotes not resolved)
Although not universal in awk, it is universal in the common shell. 3. Some other questions about local variables
① The result of a command as a variable name in inverted quotes is a common method, eg:cmd= ' date +%f ' ② uses the
$ symbol to use the result of the command as a variable name, eg:cmd=$ (date +%f)
③ variable is used in curly braces: in time, The host name, etc., is commonly used when packaging is part of the package name.
EG1: Packing with time as part of file name
cmd=$ (date +%f) //Because the ' date +%f ' is not easily recognizable, it is less likely to use ' date +%f '
tar-zcf code_$ (date+%F) _kang.tar.gz/etc//No problem
TAR-ZCF code_ ' Date +%f ' _kang.tar.gz/etc //No problem
TAR-ZCF code_kang_$cmd.tar.gz/etc //No problem
TAR-ZCF CODE_$CMD_KANG.TAR.GZ/ETC //will be ambiguous, because the system will not be clear is should parse $cmd or Cmd_kang
tar-zcf code_${cmd}_kang.tar.gz/etc //There will be no ambiguity.
The following two test results are as follows (no {} is inconsistent with the ideal result):
EG2: Packing with host name and time
cmd=$ (date +%f)
host=$ (hostname)
TAR-ZCF code_${cmd}_${host}.tar.gz/etc
Test:
Develop the habit of enclosing strings to prevent errors that are not easily discovered. 4, the Shell's special variables:
$: Gets the file name of the currently executing shell script (the full path is obtained when execution is given)
combination of two commands and $ test : Gets a path name and filename for a file with a path
DirName (get directory name portion)
basename (get filename part)
Test:
$n: Gets the nth parameter of the currently executing shell script, if n=0 gets the file name of the script. If n>9 needs to be enclosed in curly braces, eg:${21}
Test $n:
$*: Gets all the parameters of the currently executing shell and treats all command-line arguments as a single string
$#: Gets the total number of arguments in the current shell command line
$@: All parameters of this program "$" "$" "$" "...", This is the best way to pass parameters to other programs, because it preserves any whitespace $* and $@ that are embedded in each parameter
:
$* treats all parameters of the command line as a string: "$1$2$3 ..."
$@ treats each parameter of the command line as a single string: "$" "$" "$" ...
The basic tests are as follows:
Get state variable:
$$: Gets the current shell process number
$: Gets the return value that executes the previous instruction (0 is a success, Non-zero is a failure), and you can determine whether the previous command was executed successfully.
$_: The last parameter of a command or script executed prior to this
$? The variable actually gets the return value of the previous program returned to the parent process shell (the value is between 0-255:0 indicates a successful run, and 2 indicates permission denied, 1~125 is the reason for the run failure is a script command, a system command error, or a parameter pass error, and 126 is unable to perform the command, 127 for no such command/procedure, >128 indicates that the command was terminated by the system.
the different return values of $? Test:
the value range of $ is tested as follows:
$? Application in script:
Commonly used to determine whether the previous step was successful (compression failed print error compression successfully print OK):