Linux Learning Diary-using Shell variables

Source: Internet
Author: User

Shell variables are used to store the specific parameters (values) that the system and the user need to use, and these parameters can vary according to the user's settings or changes in the system environment.

"The role of variables"

Provides specific parameters for flexible management of Linux systems, with two layers of meaning:

1. Variable name: Using a fixed name, by the system preset or user-defined

2. Variable value: Can change according to user's settings and system environment

"Type of variable"

1. Custom variables: defined, modified and used by the user

2. Environment variables: maintained by the system for setting the working environment

3. Positional variables: Passing parameters to the script through the command line

4. Predefined variables: A class of variables built into bash that cannot be modified directly

"Custom Variables"

Format: variable name = variable Value

Variable names start with a letter or underscore, are case-sensitive, and are recommended for all caps

(i) Define new variables

650) this.width=650; "src=" Http:// "title=" 1.png " alt= "Wkiom1grojwa8wnraaaosxlz1_8235.png"/>

(ii) Viewing and referencing the value of a variable

Format:echo $ Variable Value

650) this.width=650; "src=" Http:// "title=" 2.png " alt= "Wkiom1gro9hrjiqcaaazzyi80yq772.png"/>

When the variable name is easily confused with the other word typeface, you need to add braces {} to enclose it, otherwise you will not be able to determine the correct variable name.

650) this.width=650; "src=" Http:// "title=" 3.png " alt= "Wkiom1grpbbtykavaaaw121eq0c477.png"/>

(c) Special operations for assigning variables

1. Double quotation marks ("): Allows you to refer to other variable values through the $ symbol, for example:

650) this.width=650; "src=" Http:// "title=" 4.png " alt= "Wkiol1grp7ejtdudaaa7shss3ik223.png"/>

2. Single quotation mark ('): Prohibit reference to other variable values, $ as ordinary characters, for example:

650) this.width=650; "src=" Http:// "title=" 5.png " alt= "Wkiom1grqf3zd06maaazwjhx9y8884.png"/>

3. Reverse apostrophe (') command substitution, extract output after command execution, for example:

650) this.width=650; "src=" Http:// "title=" 6.png " alt= "Wkiol1grrfctp8ymaaap1lt4bme099.png"/>

$ () function: Can replace the anti-apostrophe, solve the nesting problem, because the anti-apostrophe can not be nested, for example: Query the package that provides the USERADD command program installed configuration file location

650) this.width=650; "src=" Http:// "title=" 7.png " alt= "Wkiom1grrq-r1gczaaarhi4p06q253.png"/>

4. Read command: used to prompt the user to enter information

650) this.width=650; "src=" Http:// "title=" 8.png " alt= "Wkiol1grrckw9y9kaaaxuy-7-0g460.png"/>

(iv) Setting the scope of the variable

The newly defined variables are valid only in the current shell, and if you want to enter a new shell environment, you will need to export the variables as "global variables" using the Export command.

650) this.width=650; "src=" Http:// "title=" 9.png " alt= "Wkiol1grr1ohm1jraabp8ate2eg446.png"/>

You can specify more than one variable name as a parameter (you do not need to use the $ symbol), and the variable name is separated by a space

650) this.width=650; "src=" Http:// "title=" 10.png "alt=" Wkiol1grsk-b2q6kaabdyrhsdso943.png "/>

When exporting global variables using export, you can also assign values to variables, such as:

650) this.width=650; "src=" Http:// "title=" 11.png "alt=" Wkiol1grszassqthaaauij1cdxu023.png "/>

(v) operation of numerical variables

Format:expr variable 1 operator variable 2 [operator variable 3] ...

+: addition operation

-: Subtraction operation

\*: Multiplication, note that you cannot use only the "*" symbol, or you will be treated as a file wildcard

/: Division operation

%: Modulo operation, also known as take-rest operation, is used to calculate the remainder after dividing the value

650) this.width=650; "src=" Http:// "title=" 12.png "alt=" Wkiom1grs_ut6k0xaabt2i2yoa4229.png "/>

To assign the result of an operation to another variable, you can combine the command substitution operation (with the inverse apostrophe)

650) this.width=650; "src=" Http:// "title=" 13.png "alt=" Wkiom1grtmzdg2-aaaartqkdvm0323.png "/>

"Special shell variables"

1. Environment variables

Environment variables refers to a class of variables that are created in advance by the Linux system for operational needs, primarily for setting up the user's working environment, including the user's host directory, the command lookup path, the user's current directory, the login terminal, and so on.

650) this.width=650; "src=" Http:// "title=" 14.png "alt=" Wkiol1grthfxqbpoaabjzxkoumu589.png "/>

The phth variable is used to set the default search path for executable programs, for example:

650) this.width=650; "src=" Http:// "title=" 15.png "alt=" Wkiol1gryhqyym5zaabdukdml6q820.png "/>

2. Position variables

Positional variables also become positional parameters, expressed using $1,$2,$3,..., $9. For example:

Write a small script that is used to operate two integers and

650) this.width=650; "src=" Http:// "title=" 16.png "alt=" Wkiom1gryefrcfb1aaas5irw5v8660.png "/>

The two integers that need to be evaluated are provided in the form of positional variables when executing a script

650) this.width=650; "src=" Http:// "title=" 17.png "alt=" Wkiom1gry7wgsyy7aabgs6e55t8891.png "/>

3. Pre-defined variables

Predefined variables, after the system has been installed, can not be created, can only be used, for example: $ #表示命令行中位置参数的个数, $* represents the contents of all positional parameters, $? Indicates the return state after the previous command was executed, returning 0 is correct. $ A represents the name of the currently executing script or program.

650) this.width=650; "src=" Http:// "title=" 18.png "alt=" Wkiom1grz1esceydaacmbgb3zwk067.png "/>

This article is from the "Dark Cloud Technology" blog, please be sure to keep this source

Linux Learning Diary-using Shell variables

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: 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.