Bashshell variable configuration rules

Source: Internet
Author: User
The configuration code of the bashshell variable is associated with the variable content with an equal sign "", as shown below: "mynameVBird" and the equal sign cannot be directly followed by a space character, as shown below is an error: the variable names of "mynameVBird" or "mynameVBirdTsai" can only be English letters... the bash shell variable configuration code variables and variable content are connected by an equal sign "=", as shown below: "myname = VBird" and other signs cannot be directly followed by space characters, as shown below is an error: the variable name of "myname = VBird" or "myname = VBird Tsai" can only contain English letters and numbers, but cannot start with a number. The following is an error: if there is a space character in the variable content of "2 myname = VBird", you can use double quotation marks "" or single quotation marks "'" to combine the variable content, but special characters such as $ in double quotation marks, you can retain the original features, as shown below: "var =" lang is $ LANG "", then "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". the escape character "\" can be used to place special symbols (such as [Enter], $, \, space character, ', etc.) is a general character. in a string of commands, the information provided by other commands is also required, you can use the 'command' or '$ (command)' quotation marks )』. 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, if you want to obtain the core version configuration: "version = $ (uname-r)" and then "echo $ version", you can get "2.6.18-128. el5 "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 "if the variable needs to be run in other subprograms, you need to use export to convert the variable into an environment variable:" export PATH "is usually written in uppercase as the default system variable, you can use lower-case characters to configure variables by yourself (purely based on your interests and interests). to cancel a variable, use unset: "unset variable name", for example, to cancel the configuration of myname: let laruence give you a few examples under "unset myname" to show you how to configure your variables ??? /Div> 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: If 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]-c to end! [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: I want to "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 examples are missing ??? /Div> Example 4: Example 3. I want to add "yes" to the name content? [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! Have we configured the nameyes variable?? Yu?? Oh, Yu? /Div> [root @ www ~] # Name = "$ name" yes [root @ www ~] # Name =$ {name} yes <= This example is better! Example 5: How can I enable the name = VBird I just configured to use the next shell program? [Root @ www ~] # Name = VBird [root @ www ~] # Bash <= enter the so-called subroutine [root @ www ~] # Echo $ name <= subroutine: echo again; <=! There is no content just configured! [Root @ www ~] # Exit <= subroutine: exit this subroutine [root @ www ~] # Export name [root @ www ~] # Bash <= enter the so-called subroutine [root @ www ~] # Echo $ name <= subroutine: run here! VBird <= See It! The configuration value is displayed! [Root @ www ~] # Exit <= subprogram: What is "subprogram" when you leave this subprogram? That is to say, in my current shell situation, to activate another new shell, the new shell is a 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! It's good! As for the related concepts of the program, we will mention it in Chapter 1 program management! Example 6: How to enter the current core module directory? [Root @ www ~] # Cd/lib/modules/'uname-r'/kernel [root @ www ~] # Cd/lib/modules/$ (uname-r)/kernel each Linux can have multiple core versions, and almost the core versions of distribution are different. Taking CentOS 5.3 (before upgrade) as an example, his default core version is 2.6.18-128. el5, so the core module directory is in/lib/modules/2.6.18-128. el5/kernel. Because the value of each distributions is different, we can use the uname-r command to obtain the version information first. So ??? Tu TI's upper abdomen? Xiamen? Where can I ?? The 'uname-R' command first gets the version and outputs it to the cd... command, so that it can smoothly enter the directory where the current core driver is placed ?? "Why did the ghost prize come from Shao sang? /Div> In fact, the above command can be said to have done two actions, that is, first line the action "uname-r" in single quotes and get the core version 2.6.18-128. el5 will bring the above results into the original command, so the command is: "cd/lib/modules/2.6.18-128. el5/kernel/"Example 7: cancel the variable content of the name just configured [root @ www ~] # Unset name based on the above case, you can try it! Can you understand the configuration of variables ?? Why? Which of the more important special symbols are used ?? ±? Too many? Screwed ?? Screwed? Yan address? (3? How can I blow my toes? Nuo magpie? Zilumu?? /Div> Example: What is the difference between single quotation marks and double quotation marks in variable configuration? A: 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 named name = VBird, and now you want to define the content of myname to display VBird its me, how should we set it? [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! Example: What is the meaning of the symbol 'in the command issuing process? A: In a string of commands, the command within 'will be run first, and the running result will be used as external input information! For example, uname-r shows the current core version, and our core version is in/lib/modules. Therefore, you can first run uname-r to find the core version, then, run "cd directory" to this directory. of course, you can also run the content as in example 6 above ??? Nbsp; for another example, we also know that the locate command can list all the 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 File: [root @ www ~] # Ls-l 'locate crontab'. in this way, the data in the file name is first listed in locate, and then processed by the ls command !?? Pai_^ example: if you have a common working directory named "/cluster/server/work/taiwan_2005/003/", how can you simplify this directory? A: In general, if you want to enter the preceding directory, you need "cd/cluster/server/work/taiwan_2005/003/". In the case of laruence, in the numeric mode, laruence often configures a long directory name (to avoid forgetting), but it is very troublesome to change the directory. At this time, laruence is used to reduce the issue of command issuing errors in the following ways:
[Root @ www ~] # Work = "/cluster/server/work/taiwan_2005/003/" [root @ www ~] # Cd $ work
In the future, when I want to use other directories as my Mode working directory, I only need to change the work variable! This variable can be directly specified in the bash configuration file, so every time I log on, as long as I run "cd $ work", I can go to the working directory of numerical simulation! Is it convenient? Pai_^ Tips: To be honest, it is better to use "version = $ (uname-r)" to replace "version = 'uname-R'", because it is always easy to make mistakes or read errors in single quotes! So today, laruence is used to using $ (command) to introduce this function!
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.