Learn the shell of Linux with the uncle. The shell variables in the second section

Source: Internet
Author: User
Tags echo command lowercase

Shell's variable function

Variables are an important part of the shell, and we know that Linux is a multiuser, multitasking environment where everyone gets a shell when they log into the system, and everyone can use Bash to execute mail to get their own messages. The question is, how does bash know which one is your mail box? That's the thing that needs the variable.

1. What is a variable?

Simply put, a variable is a string of characters to represent an unknown or changing content. For example, to understand the variables, when there are two people in an environment, Tom and Jack two, Tom said, "How do I, you ..." Jack said, "How do I, how do you do", in their two people's dialogue "I" "You" is two variables, Everyone think of Tom said you and I, I =tom, you =jack, on the contrary, Jack said you and I =jack, you =tom, now everyone understand the variable is what? Several features of the variable:

  • Variable and convenience: for example, we each account has its own mailbox by default is the mail this variable to access, when a user login will get mail this variable, and the content of this variable is/var/spool/mail/a, If the content of this variable is/var/spool/mail/b when B is logged in, and we use the Mail command to read our mailbox, the program will automatically call the mail variable, it will be able to automatically identify their own mailbox, so that the programmer is a lot easier. Let's think about the operation of this example, when we enter the user name and password when we log in to Linux, the system automatically changed/var/spool/mail this file to/var/spool/mail/account name. When another user is logged in, pressing the ENTER key will change the contents of the file, which is the benefit of the variable. Here we need to note that the lowercase mail is the command, the uppercase mail is the variable name

  • Variables that affect the BASH environment: certain variables affect the environment, for example, the path variable that we've been referring to many times before, and whether you can execute some commands in any environment is very much related to this path variable. For example, when you execute the LS command, the system looks for the command through the path sequence in the variable that is recorded in path, and if it is not found, the system will prompt you to "command not Fount" again, because under Linux, all executions require code execution. And as mentioned above, the real shell and Linux communication is the correct login to Linux, you will have a bash execution program, which is the bash to communicate with Linux, before you log in to Linux need some variables to provide its data access, These variables are environment variables such as Path,home,mail,shell and so on, in order to distinguish environment variables from other variables we are generally capitalized.

  • Variables as a good helper for scripting: We still illustrate when we write a large script, some of the data may be due to user habits, such as the path is good, because the path in the script is used in a lot of places, if the next time we change a machine, It is particularly troublesome to modify these paths, and this time we usually use variables to make all the paths in the script change.

2. Display and setting of variables

After we know the variable, I should know what the variable looks like, and this time we use the echo command to display the contents of the variable. echo ${Variable} For example, we enter the Echo $PATH on the system, and of course the echo function is more than just displaying the contents of the variable, you can use the man echo to see it.

3. Setting rules for variables

    • The contents of variables and variables are concatenated with an equal sign "=" as follows: Myname=dashu

    • The equals sign cannot be directly connected.

    • Variable names can only be English letters and numbers, but cannot start with numbers

    • The contents of a variable can be enclosed in "" "If it contains a space, but note that the special characters of the interior of the double quotation mark can retain the original characteristics, the single quotation mark inland special characters represent the general characters

    • Escape character \ Can change some special characters to ordinary characters such as $, enter, \, space,!

    • If the contents of the variable contain other information that needs to be provided by other commands, you can use the $ (command) or ' command '

    • If you can use "$" or ${} in order to increase the contents of a variable

    • If the variable needs to be used in a sub-environment you can use export

    • In general, our uppercase characters are the default variables for the system, and the variables you name are lowercase characters.

To cancel the name of a variable we're using unset.

4. Function of environment variable

Use env to view environment variables and common environment Description: ENV is environment's abbreviation function is to list all environment variables. Of course, if you use export will be the same content, but export has other additional features, we look at these variables have what function.

    • Home on behalf of the user's main file, remember we can use the CD to go to their home folder?

    • Shell it tells us which program the shell is currently using in this environment? Linux is using the/bin/bash

    • Histsize historical size, this variable defines how much of our history command bar is saved

    • Mail box files that we read when we use the Mail command to check our mails

    • Path executes the file's lookup paths, separated by directories and directories;

    • Lang language variable, our Chinese encoding is generally zh_cn.gb2321 and utf-8

Random number variables

5. View variables with Set

Bash is not just an environment variable, but also some variables related to the operation and interface, as well as the user's own defined variables. Set all other variables inside bash are displayed in addition to the environment variables.

Setting of the PS1 prompt (not to be continued)

Learn the shell of Linux with the uncle. The shell variables in the second section

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.