Rules for using variables under Linux

Source: Internet
Author: User

    • Configuration rules for variables
  1. Variables and variable contents are linked with an equal sign "=", as follows:
    "Myname=vbird"

  2. The equals sign cannot be directly connected to a space character, as shown in the following error:
    "myname = Vbird" or "Myname=vbird Tsai"

  3. The variable name can only be an English letter and a number, but the beginning character cannot be a number, as the following is an error:
    "2myname=vbird"

  4. Variable contents If there are spaces, you can use the double quotation mark "" "or the single quotation mark" ' "to combine the contents of the variable, but the use of the caret" \ "to the special symbol (such as [Enter], $, \, space, ' etc.) into a general character;

    • The special characters within the double quotation mark such as $ etc., can retain the original characteristics, as follows:
      "var=" Lang is $LANG "" Then "echo $var" can get "Lang is en_US"
    • The special characters in single quotation marks are only ordinary characters (plain text), as follows:
      "Var= ' Lang is $LANG '" then "Echo $var" to "Lang is $LANG"
  5. In a string of commands, you also need the information provided by other commands, which can be used with the inverted single quotation mark "' Command '" or "$ (command)". In particular, that ' is the number key above the keyboard 1 the left button, not the single quote! For example, to get the configuration of the core version:
    "Version=$ (Uname-r)" and "Echo $version" can get "2.6.18-128.el5"

  6. If the variable is the content of the amplified variable, the contents can be accumulated using the "$ variable name" or the ${variable}, as follows:
    "Path=" $PATH ":/home/bin"

  7. If the variable needs to be run in another subroutine, you need to export the variable into an environment variable:
    "Export PATH"

  8. Usually uppercase characters are system default variables, self-configuring variables can use lowercase characters, easy to judge (purely according to user interests and hobbies);

  9. The way to cancel a variable is to use unset: "unset variable name", for example, to cancel the configuration of myname:
    "Unset myname"

Let the bird brother give you a few examples to let you try to see, you know how to configure your variables!

Example one: Configure a variable name with the content Vbird[[email protected] ~]#12name=vbird-bash:12name=vbird:command not found<== screen will show an error! Because you can't start with numbers! [Email protected] ~]#Name = Vbird<== is still wrong! Because there is a blank! [Email protected] ~]#Name=vbirdIt's <==ok!Example two: The subject, if the variable content is Vbird ' s name, is the variable content contains special symbols: [[email protected] ~]#Name=vbird ' s name#single and double quotes must be paired, there is only one single quote in the configuration above, so when you press ENTER, # You can continue to enter the variable contents. This is different from what we need, and it fails! # Remember, after the failure to recover please press [CTRL]-C end! [Email protected] ~]#Name= "Vbird ' s name"It's <==ok!# The command is left-to-right →, the quotes first encountered are useful, so as shown above, the single quotation marks will be invalidated! [Email protected] ~]#Name= ' Vbird ' s name 'The <== failed!# because the first two single quotes have been paired, there is a pair of pairs of single quotation marks on the back! And so it failed! [Email protected] ~]#Name=vbird\ ' s\ nameIt's <==ok!# Use a backslash (\) to jump off special characters, such as single quotes and SPACEBAR, which is OK!Example three: I want to "accumulate" in this variable of PATH:/home/dmtsai/bin this directory [[email protected] ~]#Path= $PATH:/home/dmtsai/bin[[email protected] ~]#Path= "$PATH":/home/dmtsai/bin[[email protected] ~]#Path=${path}:/home/dmtsai/bin# The above three formats are OK in the PATH configuration! But the example below is not necessarily the case!Example four: Example three, I want to add the name of the content "yes"? [Email protected] ~]#Name= $nameyes# You know what? If there are no double quotes, then what is the variable? The content of name is $nameyes this variable! Oh We are not configured to nameyes this variable! So, it should be the bottom! [Email protected] ~]#Name= "$name" Yes[[email protected] ~]#Name=${name}yes<== in this case is better!Example five: How can I just configure the Name=vbird to be used in the next shell program? [Email protected] ~]#name=vbird[[email protected] ~]# bash <== into the so-called subroutine [[email protected] ~]# echo $name <== subroutine: Echo again, <== Hey! There is no content just configured Oh! [[email protected] ~]# exit <== subroutine: Leave this subroutine [email  protected] ~]# export name[[email protected] ~]#  Bash <== into the so-called subroutine [[email protected] ~]# echo $name <== Sub-program: Run here! Vbird <== See it! The configuration value appears! [[email protected] ~]# exit <== subroutine: Leave this subroutine  

What is a "subroutine"? That is, in the case of my current shell, to activate another new shell, the new shell is a subroutine! In a general state, the custom variables of the parent program cannot be used within the subroutine. But by turning the variable into an environment variable through export, it can be applied under the sub-program! It's not bad! As for the concept of the procedure, we will refer to it in the 17th chapter of program Management.

cd/lib/modules/$ (uname-r)/kernel

Each Linux can have multiple core versions, and almost distribution core versions are different. As an example of CentOS 5.3 (not pre-upgrade), his default core version is 2.6.18-128.el5, so the core module directory is within/lib/modules/2.6.18-128.el5/kernel/. Also because each distributions value is different, but we can use uname-r this command to get the version information first. So, you can get the version output to CD through the command ' uname-r ' in the command above. In that command, you will be able to get into the current core of the driver placed in the directory! Very convenient!

In fact, the above order can be said to have made two movements, namely:

    1. First, the action "uname-r" in the inverted single quotation mark and get the core version of 2.6.18-128.el5
    2. Bring the above results into the original command, so the command is: "cd/lib/modules/2.6.18-128.el5/kernel/"
unset name

According to the above case you can try! You can understand the configuration of variables! This is very important yo! Please practice! Among them, the more important use of some special symbols! For example, single quotation marks, double quotes, skipping characters, money size, anti-single quotation marks, and so on, the example below to think about it!

Example:How is the use of single and double quotes different in the configuration of variables? Answer: Double quotes can still hold the contents of a variable, but only ordinary characters can be in single quotes, without special symbols. Let's take the following example: Suppose you define a variable, Name=vbird, now you want to define the content of myname display vbird its me with the contents of the variable name, how to set it?
[Email protected] ~]# Name=vbird
[Email protected] ~]# echo $name
Vbird
[Email protected] ~]# myname= "$name its Me"
[Email protected] ~]# echo $myname
Vbird its Me
[Email protected] ~]# myname= ' $name its me '
[Email protected] ~]# echo $myname
$name its me
Did you find it? That's right! When using single quotes, then $name will lose the original variable content, only the normal character of the display mode! There must be special care!

Example: What is the meaning of the anti-single quote (') symbol in the command release process? a : in a series of commands, the command within ' will be run first, and the result of its running will be the external input information! For example, Uname-r will show the current core version, and our core version is in/lib/modules, so you can run uname-r to find the core version, then "CD directory" to the directory, of course, you can run the same as the above example six of the running content.

For Another example, we also know that the Locate command can list all relevant file names, but what if I want to know the permissions of each file? For example, I want to know the permissions for each crontab related file name:
[[email protected] ~]# ls-l ' locate crontab '
so that the file name data is listed in locate first, and then ls command to deal with the meaning! Do you understand? ^_^

Example: If you have a regular working directory named: "/cluster/server/work/taiwan_2005/003/", how do you simplify the directory? a : Under normal circumstances, if you want to enter the above directory to "cd/cluster/server/work/taiwan_2005/003/", in the case of Brother Bird himself, Brother run the numeric pattern often configures a long directory name (to avoid forgetting), but it can be cumbersome to transform the directory. At this point, Brother Bird is accustomed to using the following way to reduce the issue of the command issued wrong:
[[email protected] ~]# work= "/cluster/server/work/taiwan_2005/003/"
[email protected] ~]# CD $work
in the future I want to use another directory as my mode working directory, just change work this variable! And this variable can be directly specified in the bash configuration file, every time I log on as long as the "CD $work" will be able to go to the working directory of numerical mode emulation! Is it convenient? ^_^

Tips:
To be honest, using "version=$ (uname-r)" to replace "version= ' uname-r" is better, because anti-single quotes are always easy to hit or wrong! So now brother Bird is accustomed to use $ (command) to introduce this feature!
    • The relationship between a custom variable and an environment variable, the difference between set and env output
    • Export: Custom variable to environment variable

Talk about Env and set now that there are so-called environment variables and custom variables, what is the difference between the two? In fact, the difference between the two is " whether the variable will be covered by the program continues to reference"! Well! So what is the parent program? Sub-Program? This will have to understand the order's release behavior.

When you log into Linux and get a bash, your bash is a standalone program, called the PID. Then any commands you place under this bash are derived from this bash, and the commands that are issued are called subroutines. We can use the diagram below to illustrate the concept of the parent program and the subroutine:


Figure 2.3.1, program correlation

As shown above, we run another bash under the original bash, and the resulting environment interface runs to the second bash (the subroutine), and the original bash is paused (asleep, sleep). The entire command runs in a solid part of the environment! To go back to the original bash, you only have to end the second bash (exit or logout). More procedural concepts We'll talk about in the fourth article, as long as there's this concept.

How does this program concept relate to variables? The relationship is BIG! Because the subroutine inherits only the environment variables of the parent program, the subroutine does not inherit the custom variables of the parent program! So you're in the original bash custom variable will disappear after entering the subroutine, until you leave the subroutine and return to the original parent program, this variable will appear again!

In other words, that is, if I can change the custom variable into an environment variable, then it is not possible to let the value of the variable continue to exist in the subroutine? Oh! That's right! At this point, the Export command is very useful! If you want the contents of the variable to continue to be used in the subroutine, then run:

Export variable Name

This thing is used in the " share your own variable configuration to the later called file or other program"! Like brother Bird often in the back of his own master file to call other ancillary files (functions like function), but the master file and the attached file has the same variable name, if repeatedly configured, to modify is also very cumbersome, at this time as long as the original first file is configured in the "Export variable", The files you call later can be configured with this variable! Without the need for duplicate configuration, this is very useful in shell script Oh! If you release only the export and not the variable, then all the "environment variables" will show up! For example:

Exportdeclare-x histsize= "Declare-x home="/root "Declare-x hostname=" Www.vbird.tsai "Declare-x inputrc="/etc/ Inputrc "Declare-x lang=" en_US "declare-x logname=" root "# The birds in the back are all directly omitted! Or.... Waste pages ~ ^_^

So how do you turn environment variables into custom variables? You can use the declare described later in this chapter!

Rules for using variables under Linux

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.