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
- Variables and variable contents are linked by an equal sign "=":
- "Myname=vbird"
- The equals sign cannot be directly connected to a space character, as shown in the following error:
- "myname = Vbird" or "Myname=vbird Tsai"
- 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"
- 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
- First, the action "uname-r" in the inverted single quotation mark and get the core version of 2.6.18-128.el5
- 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