When studying LinuxShell variables, you may encounter Shell variables. here we will introduce the use of local variables and environment variables.
When learning Linux Shell variables, you may encounter issues with using Shell variables. here we will introduce the use of local variables and environment variables, here we will share with you the usage of Linux Shell variables.
I. use of local variables
Local variables act only in the current SHELL, just like local variables. they do not affect other SHELL variables.
1. display variable values
You can use the echo command. Note that you must add the $ symbol before the variable name when using the variable.
2. clear variables
Use the unset command to clear the command.
3. display all local Linux Shell variables
Some environment variables are pre-defined in SHELL. to display these commands, you can use the set command. In addition, local variables that have been defined by the user are also displayed.
4. test whether the variable has been assigned a value.
$ {Variable:-value}
If the variable has been assigned a value, use the variable value. if the variable is null, use value. Note that value is not assigned to variable.
$ {Variable: = value}
The difference above this expression is that if variable is null, value is assigned to variable. if the variable needs to be checked to be null, a system error message is displayed, use the following expression:
$ {Variable :?}
If you need to display your own defined information, the information is as follows:
$ {Variable :? "Customed message "}
5. set read-only variables
You can use readonly to define a variable as read-only.
Readonly variable
Execute readonly to display all read-only variables.
II. environment variables
Environment variables are used for all user processes. in Linux, a login process is called a parent process, and a user program executed in shell is called a child process.
Environment variables can be set in the command line, but these values will be lost when the user logs out, so it is best to define them in. profile under the $ HOME/directory.
Traditionally, all environment variables are in uppercase. before applying environment variables to user processes, you must use the export command to export them. The Setting method is the same as that for local variables.
1. display environment variables
Use the env command to view all environment variables
2. sneak into SHELL variables
EXINIT saves the initialization options in the vi editor. For example, to display the row number, it is as follows:
EXINIT = 'set nu '; export EXINIT
HOME: used to record the user's HOME directory;
IFS: used as the default domain separator specified by shell.
PATH: stores the directory order for command or script search. You can set it in. profile.
3. export the variable to the sub-process
Use the export command in the parent script to export the variable so that the script can know the value of the variable. 54com.cn
III. location variable parameters
When passing parameters to the script, you can use this location variable to obtain the parameters. they are:
$0: Script name. This variable contains the address. you can use basename $0 to obtain the script name.
$1: The first parameter
$2, $3, $4, $5,... and so on.
IV. specific variable parameters
Control information about script running
$ # Number of parameters passed to the script
$ * Display all parameters passed to the script with a single string
$ ID of the script run
$! ID of the last process running in the background
$ @ Is the same as $ #, but it is enclosed by quotation marks and each parameter is returned in quotation marks.
$-Display the current options used by shell.
$? Display the launch status of The Last Command. 0 indicates no error.