Explanation of Linux variables

Source: Internet
Author: User
1. SHELL local variables: local variables act only in this SHELL, just like local variables. It does not affect variables in other shells. Format: NAMEvalue1, variable call: Add $ echo $ HOME/home/hbwork $ WEEKSatur $ echoToday before the variable .. 1, SHELL local variable:
 
Local variables act only in this SHELL, just like local variables. It does not affect variables in other shells.
 
Format: NAME = value
 
1. call a variable: Add $
$ Echo $ HOME
/Home/hbwork
$ WEEK = Satur
$ Echo Today is $ WEEKday
Today is
 
 
 
$ Echo Today is $ {WEEK} day // if the variable and other characters form a new word, you must add braces {} to the variable to display it more clearly to shell, which is a real variable to merge strings and other functions.
Today is Saturday
2. display variable values
You can use the echo command. Note that you must add the $ symbol before the variable name when using the variable. In addition, you can use the set command to display all local SHELL variables. Some environment variables are pre-defined in SHELL, and local variables that have been defined by the user are also displayed.
 
 
3. assign a value to the Shell variable from right to left. (if the variable is not assigned a value, shell does not report an error, but the value is null !)
$ X = $ Y y = Y
$ Echo $ X
Y
$ Z = z Y = $ Z
$ Echo $ Y
 
$
 
4. use the unset command to delete the value assignment of a variable.
$ Z = hello
$ Echo $ Z
Hello
$ Unset Z
$ Echo $ Z
 
$
 
5. conditional command replacement (test whether the variable has been assigned a value)
In the Bourne Shell, variable replacement can be executed under specific conditions, that is, conditional environment variable replacement. This type of variable replacement is always enclosed in braces.
 
When the $ {variable:-value} variable is replaced, the default value defined in the command line is used, but the value of the variable is not changed. Variable is a variable value, and value is the default value used for variable replacement.
 
Example: $ echo Hello $ UNAME
Result: Hello
$ Echo Hello $ {UNAME:-there}
Result: Hello there
$ Echo $ UNAME
Result: (null)
$ UNAME = John
$ Echo Hello $ {UNAME:-there}
Result: Hello John
 
$ {Variable: = value} not only replaces the value with the default value, but also assigns the default value to the variable. After the variable is replaced, the value is given to the variable.
 
Example: $ echo Hello $ UNAME
Result: Hello
$ Echo Hello $ {UNAME: = there}
Result: Hello there
$ Echo $ UNAME
Result: there
 
The variable replacement value can also be enclosed:
 
$ USERDIR = {$ Mydir:-'pwd '}
 
$ {Variable: + value} is replaced with the specified value only when the variable has been assigned a value. if the variable has been assigned a value, the value is replaced with the value. otherwise, no replacement is performed.
 
Example: $ ERROPT =
$ Echo $ {ERROPT: + "Error tracking is acitive "}
The result shows that Error tracking is acitive.
$ ERROPT =
$ Echo $ {ERROPT: + "Error tracking is acitive "}
Result: (null)
 
$ {Variable :? Message} use the condition of the error check to replace the variable. when the variable has been set, it is replaced normally. Otherwise, the message will be sent to the standard error output (if this replacement occurs in the shell program, the program will terminate ).
 
Example: $ UNAME =
$ Echo $ {UNAME :? "Uname has not been set "}
The result shows: UNAME HAS NOT BEEN SET
 
$ UNAME = Stephen
$ Echo $ {UNAME :? "Uname has not been set "}
 
The result shows that the error message is "*.
If no message is specified, shell displays a default message, for example:
 
$ UNAME =
$ Echo $ {UNAME :?}
The result shows: sh: UNAME: parameter null or not set.
 
6. special settings
 
Readonly variable name
 
Sometimes, when we want to describe a variable and set it to a specific value without changing its value, we can use the following command to ensure the read-only of a variable: execute readonly to display all read-only variables.
 
Export variable name
 
At any time, the created variables are only local variables of the current Shell, so they cannot be used by other commands or Shell programs run by the Shell, the export command can provide a local variable to other commands executed by Shell, or assign a value to the variable while using the export command (export variable name = variable value ). Variables described in export can be accessed in all commands or programs run after Shell.
 
 
 
 
2. Shell parameters
 
1. location parameters
 
The parameters provided by the system are called location parameters. The value of the location parameter can be obtained using $ N. N is a number. if it is 1, it is $1. similar to the array in C language, when shell explains the user's command, the first word of the command line is used as the command, while other words are used as the parameter. When the executable file corresponding to the command is a Shell program, these parameters are transmitted to the program as location variables. The value 0th is the name of the program. it indicates the parameter passed to the program from 1. The first parameter is recorded as $1, and the second parameter is $2... the ninth parameter is $9. Among them, 1 to 9 are the real parameter names, and the "$" character is only used to identify the replacement of variables.
 
2. internal parameters
 
In the above process, $0 is an internal variable, which is mandatory and represents the program itself, while $1 is dispensable. There are several internal variables that are the same as $0.
 
$ # ---- Total number of parameters passed to the program
$? ---- When the previous code or shell program exits in the shell, if it exits normally, 0 is returned, and vice versa.
$ * ---- A string consisting of all parameters passed to the program.
$----- Options are provided when Shell is started or the set command is used
$? ---- The value returned after the previous command is executed
$ ---- Process ID of the current shell
$! ---- Process number of the previous sub-process
$ @ ---- All parameters, each enclosed in double brackets
$ N ---- location parameter value, where n represents Location
$0 ---- Current shell name
 
 
 
For example, create a program P1 with the following content:
 
Echo "Program name is $0"
Echo "There are totally $ # parameters passed to this program"
Echo "The last is $ ?"
Echo "The parameters are $ *"
 
The execution result is as follows:
 
[Beichen @ localhost bin] $ P1 this is a test program // Pass 5 parameters
 
Program name is/home/beichen/bin/P1 // give the complete path and name of the Program
 
There are totally 5 parameters passed to this program // The total number of parameters
 
The last is 0 // program execution result
 
The parameters are this is a test program // returns a string 0 consisting of parameters
 
 
 
 
 
 
III. environment variables:
 
Shell environment variables are accepted by all shell programs. When the shell program runs, it receives a set of variables, which are environment variables that act on all user processes. In Linux, the login process is called the parent process, and the user programs executed in shell are called child processes.
 
Environment variables can be set in the command line, but these values will be lost when the user logs out. Therefore, it is best to define it 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 of the local variable.
 
1. display environment variables
 
Use the env or printenv command to view all environment variables.
 
2. 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.
 
If you want to make all other shell programs use the defined variables, that is, to define new environment variables, you only need to use the export keyword.
 
3. common Shell environment variables include:
 
HOME is used to save the full path name of the registration directory.
 
PATH is used to save the directory PATH names separated by colons. Shell searches these directories in the order given in the PATH variable, and the first executable file with the same name as the command will be executed.
 
The type of the TERM terminal.
 
The identifier of the current user. The value is a string consisting of digits.
 
The absolute path name of the current working directory of PWD. The value of this variable varies with the use of the cd command.
 
PS1 primary prompt. for privileged users, the default primary prompt is #. for common users, the default primary prompt is $.
 
 
 
 
 
Appendix:
 
Bash internal commands
 
The bash command explains that the package contains some internal commands. Internal commands are invisible in the directory list. they are provided by the Shell itself. Common internal commands include echo, eval, exec, export, readonly, read, shift, wait, and point (.). The following describes the command format and functions.
 
1. echo
 
Command format: echo arg
 
Function: displays the string specified by arg on the screen.
 
2. eval
 
Command format: eval args
 
Function: when the Shell program runs the eval statement, the Shell reads the args parameter and combines them into a new command and then runs it.
 
3. exec
 
Command format: exec command parameters
 
Function: when the Shell executes the exec statement, it does not create a new sub-process, but instead executes the Specified Command. when the specified command is executed, the process (that is, the initial Shell) is terminated, so the statements after exec in the Shell program will not be executed.
 
4. export
 
Command format: export variable name or: export variable name = variable value
 
Function: Shell can use export to bring its variables down to the sub-Shell, so that the sub-process inherits the environment variables in the parent process. However, the sub-Shell cannot use export to bring its variables up to the parent Shell.
 
Note: the export statement without any variable name will display all the current export variables.
 
5. readonly
 
Command format: readonly variable name
 
Function: identifies a user-defined Shell variable as unchangeable. The readonly command without any parameters will display all read-only Shell variables.
 
6. read
 
Command format: read variable table
 
Function: Read a row from a standard input device, break it into several words, and assign values to variables defined in the Shell program.
 
7. shift statement
 
Function: the shift statement renames all location parameter variables as follows, that is, $2 becomes $1, $3 becomes $2... Every shift statement used in the program makes all the location parameters move one position to the left in sequence, and the location parameter $ # minus 1 until it is reduced to 0.
 
8. wait
 
Function: enables Shell to wait for all sub-processes started in the background to end. The return value of wait is always true.
 
9. exit
 
Function: exit the Shell program. After exit, you can selectively specify a digit as the return status.
 
10. "." (point)
 
Command format:. Shell program file name
 
Function: enables Shell to read the specified Shell program file and execute all statements in the file in sequence.
 
 
 
 
 
How to run a Shell program
 
You can use any editing program to write Shell programs. Because the Shell program is interpreted and executed, it does not need to be compiled into the target program. According to the Shell programming convention, for example, the first line of the program is generally "#! /Bin/bash ", where # indicates that this row is a comment and an exclamation point! Tell the Shell to run the command after the exclamation mark and use the rest of the document as the input, that is, run/bin/bash and let/bin/bash execute the Shell program content.
 
There are three methods for executing Shell programs.
 
1. bash Shell program file name
 
This is actually to call a new bash command to explain the program, and pass the Shell program file name as a parameter to it. The newly started Shell reads the specified file and runs the commands listed in the executable file. when all the commands are executed, the execution ends. The advantage of this method is that the Shell debugging function can be used.
 
2. bash <Shell program name
 
In this way, the Shell command interpreter uses the specified program file to redirect input.
 
3. run the chmod command to make the Shell program executable.
 
Whether a file can run depends on whether the content of the file is executable and the file has the execution right. For Shell programs, when a file is generated using the editor, the system grants 644 (rw-r --) permission. Therefore, when you need to run this file, you only need to directly type the file name.
 
Among the three methods for running Shell programs, it is best to choose the following method: when a Shell program is just created and its correctness is not yet grasped, the first method should be used for debugging. When a Shell program has been debugged, use the third method to fix it. later, you only need to enter the corresponding file name and can be called by another program.
 
Bash program debugging
 
Errors are inevitable during programming. sometimes, debugging takes more time than programming, as does Shell.
 
Shell program debugging mainly uses bash commands to explain program selection items. Bash is called in the form:
 
Bash-select the Shell program file name
 
Several common options are:
 
-E. exit immediately if a command fails.
 
-N reads commands but does not execute them.
 
-During u replacement, unconfigured variables are considered as errors.
 
-V displays the Shell input rows.
 
-X displays commands and their parameters when executing commands.
 
All the options above can also be referenced in the Shell program in the form of "set-select item", and "set + Select item" will disable this option. If you only want to use some selection items for a part of the program, you can enclose this part with the preceding two statements.
 
(1) unset variable exit and exit now
 
The unset variable exit feature allows you to check all variables. If an unassigned variable is referenced, Shell program execution is terminated. Shell usually allows the use of unspecified Variables. in this case, the variable value is null. If you have set an unset variable to exit the selection item, once you have used the unset variable, the error message is displayed and the program is terminated. Unset variable exit select item-u.
 
When a Shell is running, if a command does not exist or cannot be executed, the redirection fails, or the command ends abnormally, the error message is displayed on the terminal screen without redirection, the Shell program continues to run. To force the Shell program to end immediately when an error occurs, you can use the-e option to terminate the execution of the Shell program immediately.
 
(2) Shell program tracking
 
The main method to debug a Shell program is to use Shell commands to explain the program's-v or-x options to track program execution. -V selects the item so that the Shell will display every command line it reads during the execution of the program, the-x option enables Shell to display every command it executes in the process of executing the program with a + + command name at the beginning of the line. And display each variable and the value of the variable. Therefore, the main difference between them is that-v is not available before the command line is executed, and the original contents of the command line are displayed, if-v exists, the content of the replaced command line is displayed.
 
In addition to Shell-v and-x options, you can also take some auxiliary debugging measures within the Shell program. For example, you can use the echo command to display necessary information in some key aspects of the Shell program. its function is equivalent to the printf statement in the C language, in this way, you can know where the program runs and the current state of the program.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.