Variables of shell scripts

Source: Internet
Author: User

Common shell variables are: custom variables, environment variables, positional variables, predefined variables, and so on.

Variables: Can be stored in a variable value of space, can be changed by different environments, is a variable value. Echo output variables are generally used.

Variable format:echo $ variable name instance as follows:

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/83/9E/wKioL1d4tV3Q2LT7AACrmlDglV0010.jpg-wh_500x0-wm_3 -wmp_4-s_3965358442.jpg "title=" qq20160703144817.jpg "alt=" Wkiol1d4tv3q2lt7aacrmldglv0010.jpg-wh_50 "/>

By assigning a value, you omit the tedious input path. This also can be the perfect output, to see the requirements of the network card. This is the advantage of variables.

echo $? (? To detect if the command is successful,)

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/83/9F/wKiom1d4tgGz5DpkAABUi73pZr0880.jpg-wh_500x0-wm_3 -wmp_4-s_4155353959.jpg "title=" qq20160703145128.jpg "alt=" Wkiom1d4tggz5dpkaabui73pzr0880.jpg-wh_50 "/>

The last command output is correct, execute echo $? Then output 0, if not correct, output a nonzero number .

Custom variables:

Custom variables are variables that users define themselves according to their own environment.

Bash A simple variable, instead of declaring it in advance, assigns the variable name directly and assigns it to the initial value;

The basic format of a custom variable is the variable name = variable value equals two times no spaces are allowed;

Variable names can only be preceded by a letter and an underscore name cannot contain +,-*,/. , 、 ? % * ...... And so on some special characters.

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/83/9F/wKiom1d4uK6TfdsYAAAmrNCKtY8119.jpg-wh_500x0-wm_3 -wmp_4-s_1802472371.jpg "title=" qq20160703150226.jpg "alt=" Wkiom1d4uk6tfdsyaaamrnckty8119.jpg-wh_50 "/>

The above is to customize a variable, and assign a value, the custom variable must pay attention to the case.

Can be defined directly at the command line, the above is to define a variable and give the value, through the ECHO output variable $ is the special character of the reference variable ( must use the $ symbol ).

Echo There must be a space between the variable and the call.

use echo directly after the variable called with $ if there are multiple spaces separated by

Example 2:

variable Assignment: The new value assigned by the following will overwrite the previous assignment directly.

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/83/9E/wKioL1d4vgiTD8_sAAAh6aFCUWA592.jpg-wh_500x0-wm_3 -wmp_4-s_2842184597.jpg "title=" qq20160703152545.jpg "alt=" Wkiol1d4vgitd8_saaah6afcuwa592.jpg-wh_50 "/>

Example 3:

You should use {} to enclose the variable name when the variable name and subsequent characters are easily confused.

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/83/9F/wKioL1d4xnCzMQ2tAAAriZ81F_E426.jpg-wh_500x0-wm_3 -wmp_4-s_1866930818.jpg "title=" qq20160703160033.jpg "alt=" Wkiol1d4xnczmq2taaariz81f_e426.jpg-wh_50 "/>

Double quotation marks (")

When a space is assigned to the right of the = sign, enclose it in double quotation marks,

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/83/9F/wKioL1d4yKajecctAAAe6KLZn7M652.jpg-wh_500x0-wm_3 -wmp_4-s_1867093805.jpg "title=" qq20160703161101.jpg "alt=" Wkiol1d4ykajecctaaae6klzn7m652.jpg-wh_50 "/>

You can also reference other variables within the range of double quotes, allowing you to assign existing variables to the new variable

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/83/9F/wKioL1d4yTvT01AkAAAXlv7SPx0096.jpg-wh_500x0-wm_3 -wmp_4-s_2345377211.jpg "title=" qq20160703161335.jpg "alt=" Wkiol1d4ytvt01akaaaxlv7spx0096.jpg-wh_50 "/>

Single quotation mark (')

When the content to be assigned includes "$", "\", and so on, special characters with other meanings,

You should enclose it in single quotation marks.

Other values cannot be referenced within the single quotation mark range, any word nonspacing as a normal character, but the contents of the assignment contain single quotation marks that need to be escaped with the \ ' symbol to avoid collisions.

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/83/A0/wKiom1d4ynGwdlQmAAAkiYFxvBY989.jpg-wh_500x0-wm_3 -wmp_4-s_3461460177.jpg "title=" qq20160703161840.jpg "alt=" Wkiom1d4yngwdlqmaaakiyfxvby989.jpg-wh_50 "/>

Anti-apostrophe (')

Press the key below the keyboard ESC.

The inverse apostrophe is primarily used for command substitution, allowing the output of a command's screen to be assigned to a variable.

The anti-apostrophe must be an executable command within the range. Otherwise there will be an error

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/83/A0/wKiom1d4yzHD4JJPAAAjUCxfeTU091.jpg-wh_500x0-wm_3 -wmp_4-s_2534248909.jpg "title=" qq20160703162154.jpg "alt=" Wkiom1d4yzhd4jjpaaajucxfetu091.jpg-wh_50 "/>

The inverse apostrophe is equivalent to $ ()

It is important to note that it is difficult to use a backslash to implement a nested command in a command , and then $ () can be substituted for the anti-apostrophe

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/83/9F/wKioL1d4zZCBBbG-AAAuQe1Vj0k638.jpg-wh_500x0-wm_3 -wmp_4-s_371332219.jpg "title=" qq20160703163202.jpg "alt=" Wkiol1d4zzcbbbg-aaauqe1vj0k638.jpg-wh_50 "/>

4 ) Read command

In addition to the above assignment you can use the Read command to assign values,theRead command is used to prompt the user to enter information, so as to achieve a simple interactive process (in fact, we entered the command is an interactive process)

You need to read a line from the standard input device keyboard and use a space as a delimiter when executing

For example, I define two variable operations as follows

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/83/A0/wKiom1d40m_jpbdFAAAl7-csa4I424.jpg-wh_500x0-wm_3 -wmp_4-s_2663741858.jpg "title=" qq20160703165249.jpg "alt=" Wkiom1d40m_jpbdfaaal7-csa4i424.jpg-wh_50 "/>

For more interactive images, improve ease of use, plus the-p option to set the cue message

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/83/9F/wKioL1d40vGAIX1TAAAcwqxNtjQ703.jpg-wh_500x0-wm_3 -wmp_4-s_3849336227.jpg "title=" qq20160703165459.jpg "alt=" Wkiol1d40vgaix1taaacwqxntjq703.jpg-wh_50 "/>

The above actions are only valid in the current bash environment, and will not be valid for other consoles or other shel.

We enter the child shell of the current shell.

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/83/A0/wKioL1d407fzSNuKAAAnDBZ8OCo681.jpg-wh_500x0-wm_3 -wmp_4-s_792487379.jpg "title=" qq20160703165818.jpg "alt=" Wkiol1d407fzsnukaaandbz8oco681.jpg-wh_50 "/>

variables that are locally defined after entering the child shell will not take effect . It can only be explained that there are no output variables in the current bash

View all current variables by set

View global variables with env

Exit exit the current bash back to the parent bash

Change a local variable to a global variable

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/83/A1/wKiom1d41GLQ_fTZAAAanGffhwQ441.jpg-wh_500x0-wm_3 -wmp_4-s_2698368164.jpg "title=" qq20160703170104.jpg "alt=" Wkiom1d41glq_ftzaaaangffhwq441.jpg-wh_50 "/>

Local variables are changed to global variables through export.

You can also define a global variable and assign a value

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/83/A0/wKioL1d41eKDpcE9AAAgegqVqX0416.jpg-wh_500x0-wm_3 -wmp_4-s_2077400033.jpg "title=" qq20160703170733.jpg "alt=" Wkiol1d41ekdpce9aaagegqvqx0416.jpg-wh_50 "/>

It is important to note that the names of the variables are strictly case-sensitive


The above are: custom variables, environment variables in the experimental process. I hope I can help you.

This article is from the "11732822" blog, please be sure to keep this source http://11742822.blog.51cto.com/11732822/1795348

Variables of shell scripts

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.