Shell-based variables

Source: Internet
Author: User

Shell-based variables
Variable type
Shell variables include environment variables, location variables, pre-defined variables, and custom variables.
Environment Variable
Shell environment variables are a type of Shell predefined variables used to set the variables of the system and script runtime environment, which are uniformly named by the system. The values of environment variables are partially set by the system, the other part can be defined by the user. The environment variable name consists of uppercase letters. Common environment variables include:
HOME: the absolute path of the user's HOME directory. PATH: this variable defines the PATH of Shell's default search command. Its value is a series of directories separated by colons. For example, for common ls commands, the script program is placed in the/bin directory, but we do not need to switch to this directory when using the ls command; instead, we can use ls in any directory, this is because we add the/bin value to the PATH environment variable. Shell can actively search for corresponding programs or scripts in this directory. PWD: the absolute path of the current working directory. PS1: Primary prompt. It is necessary to understand the meaning of the characters before commands on the terminal, as follows:

M @ sys :~ $ Cd/home/
M @ sys:/home $

The first is the username m, followed by the host name sys, ':', followed by the current directory, followed by the main command prompt' ′. ' 'Is the primary prompt of a common user. For a root user,' # 'is as follows:

M @ sys:/home $ su root
Password:
Root @ sys:/home #
PS2: Secondary prompt. The default value is '> '. This auxiliary prompt appears when you need to break down the command into several rows. You only need to enter '\' At the end of the row. Then, press enter to continue the input in the next line instead of executing the command, as shown below:

Root @ sys:/home # ls | grep \
> 'M'
M
Root @ sys:/home #
LOGNAME: the user name currently logged on.
Env or set directory can be used to display and set environment variables. Location variable
When executing commands or scripts, we often follow some parameters. How do we reference these variables in shell? This is the use of location variables. The name is 0, 1, 2... where 0 corresponds to the command or executed script itself, 1 N corresponds to the nth parameter,N <= 9So there are only 10 location parameters in total.
What if I have more than 10 parameters? What should I do with 11th parameters? At this time, we need the shift command. The last location variable is 9, and it can only correspond to 10th parameters, if you want it to correspond to 11th parameters, you can only make the original first location parameter invalid, so as to move the following parameters to one place, thus 1 corresponds to the second parameter, and so on. In the same way, only a few more shift commands are required for parameters 12th and later.Note that the shift command does not enable 0 invalid, 0 always corresponds to the script or command itself.Predefined Variables
$ # The number of actual parameters, excluding the string consisting of $0 $ * all actual parameters, that is, all parameters after the script or command are treated as a string assigned to this variable. $? A decimal number indicates the exit status after the previous command is executed. If the execution is successful, 0 is generally returned. $ PID: the current process number. $! The process number of the previous command. Custom Variables
Name
It must start with a letter or underline. It can be followed by any number of letters, numbers, and underscores. It is case sensitive. To distinguish commands, we recommend that all letters be capitalized. Create
The declare and typeset commands can create variables, but they are not used unless otherwise required. The simplest and most efficient way to create a variable is to assign a value to the variable while defining the variable, similar to C language initialization. The method is to directly write the equal sign after the variable name and then keep up with the value to be assigned,It must be noted that there cannot be spaces before and after the equal signI have tried this kind of mistake several times, and it is tears to say more...
In addition, if the variable value contains spaces, You need to enclose the entire value in double quotation marks. Use
When referencing a variable, you must add the symbol' ', Of course, this is the most common method. In addition, there are several ways:" Var" Var" {Var }"
Adding braces is helpful for determining the boundary of the variable name. Otherwise, a variable myvar is defined, which will be confused during use:

Myvar = 10
Echo $ myvarth

The intention is to display 10 th, and the result may regard $ myvarth as a new variable.

So much for the time being. I will try again later.

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.