Shell variable)

Source: Internet
Author: User
Use of variables: Echo
 
[Root @ WWW ~] #Echo$ Variable [root @ WWW~] #Echo$ Path/Usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/Bin [root @ WWW~] #Echo$ {Path}

 

Variable Configuration
 
[Root @ WWW ~] #Echo$ Myname<=There is no data here ~ This variable is not configured yet! It is empty! [Root @ WWW~] # Myname =Vbird [root @ WWW~] #Echo$ Mynamevbird<= Yes! This variable has been configured!

Variable configuration rules

  1. The variable and the variable content are connected with an equal sign "= "., As shown below:
    "Myname = vbird 』

  2. Equal signs cannot be directly followed by space characters, As shown in the following code:
    "Myname = vbird" or "myname = vbird Tsai 』
  3. The variable name can only contain English letters and numbers,The START character cannot be a number.The following error is returned:
    "2 myname = vbird 』
  4. If there is a space character in the variable content, you can use double quotation marks "or single quotation marks" '"to combine the variable content,
    • Special characters in double quotation marks, such as $, can retain the original features, As shown below:
      For "Var =" Lang is $ Lang "", "Echo $ Var" can obtain "Lang is en_us 』
    • Special characters in single quotes are only common characters (plain text), As shown below:
      For "Var = 'lang is $ Lang '", "Echo $ Var" can obtain "Lang is $ Lang 』
  5. AvailableEscape Character 『\』Convert special characters (such as [enter], $, \, space character, and ') into general characters;
  6. In a string of commands, you also need information provided by other commands, you canUse the 'command' or '$ (command)' back to the single quotes )』. Note that the one on the left of the number key 1 on the top of the keyboard is not a single quotation mark! For example, to obtain the configuration of the core version:
    "Version = $ (uname-R)" and "Echo $ version" can get "2.6.18-128. EL5 』
  7. If the variable is an expanded variable, you can use "$ variable name" or "$ {Variable}" to accumulate the content, as shown below:
    "Path =" $ path ":/home/bin 』
  8. If the variable needs to be in another sub-ProgramRun, thenAn export is required to convert the variable to an environment variable.:
    "Export path 』
  9. Generally, upper-case characters are default variables of the system. You can use lower-case characters to configure variables for easy judgment (purely based on user interests and hobbies );
  10. To cancel a variable, use unset.: "Unset variable name", for example, canceling the configuration of myname:
    "Unset myname 』

Let laruence give you a few examples to show you how to configure your variables!

 Example 1: configure a variable name with the content vbird [root @ WWW ~] #12 name = Vbird -Bash: 12 name = vbird: Command not found <=An error is displayed on the screen! Because it cannot start with a number! [Root @ WWW ~] # Name = vbird <= Or error! Because there is blank space! [Root @ WWW ~] # Name = vbird <= OK! Example 2: assume that the variable content is vbird.  '  S name, that is, when the variable content contains a special symbol: [Root @ WWW ~] # Name = vbird '  S name  # Single quotation marks and double quotation marks must be paired. In the preceding configuration, there is only one single quotation mark. Therefore, when you press enter, # You can continue to enter the variable content. This is different from the functions we need and fails! # Remember, to restore after failure, press [CTRL] - End of C! [Root @ WWW ~] # Name = "  Vbird's name " <= OK! # The command is to find from the left to the right → the quotation marks first are useful, so as shown above, single quotation marks will be invalid! [Root @ WWW ~] # Name = '  Vbird  ' S name '  <== Failed!  # Because the first two single quotes are paired, an unpaired single quotation mark is added! Therefore, it fails! [Root @ WWW ~] # Name = vbird \ '  S \ name <= OK!  # Use the backslash (\) to escape special characters, such as single quotes and space keys. This is also OK! Example 3: "accumulate" in the PATH variable 』: /Home/dmtsai/ Bin directory [root @ WWW ~] # Path = $ path:/home/dmtsai/ Bin [root @ WWW ~] # Path = "  $ Path  " :/Home/dmtsai/ Bin [root @ WWW ~] # Path =$ {path}:/home/dmtsai/ Bin # the above three formats are all set in path! But the following example is missing! Example 4: Example 3: I want to add more content to the name  "  Yes  "  What about it? [Root @ WWW ~] # Name = $ Nameyes # OK? If there are no double quotation marks, what is the variable? The content of name is the variable $ nameyes! # Haha! We have not configured the nameyes variable named success! So it should be the bottom line! [Root @ WWW ~] # Name ="  $ Name  "  Yes [root @ WWW ~] # Name =$ {name} Yes <= This is a better example! Example 5: how to make the name I Just configured = Can vbird use the next shell program? [Root @ WWW ~] # Name = Vbird [root @ WWW ~] # Bash <= Go to the so-called subroutine [root @ WWW ~] # Echo $ Name <= Subroutine: Next Echo  Next; <= Hey! There is no content just configured! [Root @ WWW ~] # Exit <=Subroutine: Leave This subroutine [root @ WWW ~ ] # Export name [root @ WWW ~] # Bash <= Go to the so-called subroutine [root @ WWW ~] # Echo $ Name <= Subroutine: run here! Vbird <= Look! The Configuration value is displayed! [Root @ WWW ~] # Exit <= Subroutine: exit this subroutine

In general, the custom variables of the parent program cannot be used in the subroutine. However, after the variables are changed to environment variables through export, they can be applied under the subroutine!

 

Unquoted
How do I enter your current Core Module Directory? [Root @ WWW~] # Cd/lib/modules /'Uname-R '/Kernel [root @ WWW~] # Cd/lib/modules/$ (Uname-R)/kernel

 

Cancel variable
 
Cancel the variable content of the name you Just configured [root @ WWW~] # Unset name

 

Single quotation marks and double quotation marks
The biggest difference between single quotes and double quotes is that double quotes can still retain the content of variables, but single quotes can only be general characters without special symbols. Let's explain in the following example: Suppose you have defined a variable, name = Vbird. How can I define the content of "vbird its me" in myname? [Root @ WWW ~] # Name = Vbird [root @ WWW ~] # Echo $ Namevbird [root @ WWW ~] # Myname = "  $ Name its me  "  [Root @ WWW ~] # Echo  $ Mynamevbird its me [root @ WWW ~] # Myname = '  $ Name its me  '  [Root @ WWW ~] # Echo  $ Myname $ name its me found? That's right! When single quotes are used, $ name will lose the original variable content, only the display type of common characters! Be especially careful here 

 

Valid range of variables

What? Variables also use the "range 』? That's right ~ We mentioned this concept in the preceding export command description. If there is a different program relationship between the parent program and the subroutine when running the program, can the "variable" be referenced to be related to the export. The variable after being export can be called "environment variable 』! Environment variables can be referenced by the quilt program, but other custom variables do not exist in subprograms.

 

Declare/typeset

The declare and typeset functions are the same 『Declaring the type of a Variable』. If declare is followed by no parameters, Bash will actively call out all the variable names and contents, just like using set! So what syntax does declare have? Let's see first:

[Root @ WWW ~] # Declare [- Aixr] variable options and parameters: - A: Define the variable named variable as the array type. -I: Define the variable named variable as an integer. - X: similar to export, it is used to change the subsequent variable to an environment variable; - R: configure the variable to the readonly type. The variable cannot be changed or unset. Example 1: Make the variable  Sum Proceed 100 + 300 + 50  [Root @ WWW ~] # Sum = 100 + 300 + 50  [Root @ WWW ~] # Echo $ Sum  100 + 300 + 50 <= Success! Why didn't I calculate the sum for me? This is the variable attribute of the text type! [Root @ WWW ~] # Declare-I Sum = 100 + 300 + 50  [Root @ WWW ~] # Echo $ Sum 450 <== What ??

By default, Bash has several basic definitions of variables:

    • The default variable type is "string". Therefore, if the variable type is not specified, 1 + 2 is a "string" instead of a "formula 』. That is why the first running result will occur;
    • In the bash environment, numeric operations can only reach the integer form by default, so the 1/3 result is 0;

Now you know why you need variable declaration? If you need a variable of the non-string type, you must declare the variable! Continue to use some other declare functions.

Example 2: Set Sum  Change to environment variable [root @ WWW ~] # Declare-x Sum  [Root @ WWW ~] # Export | Grep   Sum  Declare -IX Sum ="  450  " <= It appears! Including announcements with I and X! Example 3: Let  Sum  It becomes a read-only attribute and cannot be changed! [Root @ WWW ~] # Declare-R Sum  [Root @ WWW ~] # Sum = Tesgting -Bash: Sum : Readonly variable <= Good God ~ You cannot change this variable! Example 4: Let  Sum  Change to a custom variable that is not an environment variable! [Root @ WWW ~] # Declare + x Sum <= Convert-to + You can perform the "cancel" action [root @ WWW ~] # Declare-P Sum <=- P can separately list the types of variables. -IR Sum = "  450  " <= See! Only the I and r types are left, not X!

Declare is also a useful feature ~ Especially when we need to use the Array Function, it can also help us declare the attribute of the array! However, arrays are also commonly used in shell scripts! Interestingly, If you accidentally set the variable to "read-only", you usually have to log out and then log in to restore the type of the variable!

 

Array variable type

In some cases, we must use arrays to declare some variables. What are the advantages of this? In the use of ordinary people, it cannot be seen that there is any benefit! However, if you have ever written a program, you will understand the meaning of the array ~ Array is one of the key points of learning for designers who write numeric programs! Good! Do not renew your subscription ~ So how should we configure the array variables and content? In bash, the array configuration method is:

 
VaR [Index] = content

It means that I have an array named var, and the content of this array is Var [1] = James, VAR [2] = Daming, vaR [3] = haoming .... wait, the index is a number, and the key is to use the scratch sign ([]) for configuration. Bash currently provides a one-dimensional array. To be honest, if you don't need to write some complicated programs, you can skip this array and study it later! It is of great significance to create an array, which is usually used in interactions with loops or other types!

Example: configure the VaR [ 1 ] ~ VaR [ 3  . [Root @ WWW ~] # Var [ 1 ] = "  Small min  "  [Root @ WWW ~] # Var [ 2 ] = "  Big min  "  [Root @ WWW ~] # Var [ 3 ] = "  Nice min  "  [Root @ WWW ~] # Echo   "  $ {Var [1]}, $ {var [2]}, $ {var [3]}  "  Small min, big min, Nice Min

 

From http://vbird.dic.ksu.edu.tw/linux_basic/0320bash_2.php

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.