Linux Learning Shell

Source: Internet
Author: User

Reference: http://cn.linux.vbird.org/linux_basic/0320bash_1.php

Shell variable function : A variable is a set of words or symbols, etc., to replace some configuration or a string of reserved data!

Echo This command to take the variable: Echo $PATH, you need to add $ before the variable name. The man command allows you to see how commands are used, such as: Man Echo

    • Configuration rules for variables
    1. Variables and variable contents are linked by an equal sign "=":
    2. "Myname=vbird"
    3. The equals sign cannot be directly connected to a space character, as shown in the following error:
    4. "myname = Vbird" or "Myname=vbird Tsai"
    5. 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:
    6. "2myname=vbird"
    7. 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

double quotes within the special words such as $, etc., can retain the original characteristics : "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): "var= ' Lang is $LANG '" then "echo $var" can get "Lang is $LANG"

8. Use the caret character "\" to turn special symbols (such as [Enter], $, \, space, ' etc.) into general characters; sort of like an escape character .

9. In a string of commands, you also need to provide information by other commands that 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"

10. If the variable is the content of the amplified variable, the contents can be added by the "$ variable name" or the ${variable}: " path=" $PATH ":/home/bin"

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

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!

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

13. To cancel a variable, use unset : "unset variable name", for example, to cancel the configuration of myname: "Unset myname"

How to be able to kernel modules directory: [[email protected] ~]# cd/lib/modules/' uname-r '/kernel

    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 order, so the command is: "Cd/lib/modules/2.6.18-128.el5/kern
Example: What is the difference between a single quote and a double quote in the configuration of a variable? A: The maximum difference between a single quote and a double quote is that the double quotation marks can still hold the contents of the 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
Did $name its me
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: In the process of command release, the anti-single quotation mark (') this symbol represents the meaning? 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 first in locate Out, and then the LS command to deal with the meaning! Do you understand? ^_^

Example: If you have a regular working directory name: "/cluster/server/work/taiwan_2005/003/", how to simplify the directory? A: In general, if you want to enter the above directory to "cd/cluster/server/work/taiwan_2005/003/", in the case of Brother Bird's own, the bird brother run numerical mode often configures a long directory name (to avoid forgetting), But changing the directory is a hassle. 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
future I want to use a different 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? ^_^

---restore content ends---

Linux Learning Shell

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.