Shell basic Application (ii) use of shell variables

Source: Internet
Author: User

    1. Custom variables: There are variables that are defined by the system user, only valid in the user's own shell environment, also known as local variables, and when shell scripting is programmed, certain custom variables are usually set to suit the various changes in the program execution and meet different requirements.

    2. To define a new variable:

To define the format of a variable variable name = variable value Remember that there are no spaces on either side of the equal sign, the name of the variable requires a first letter or an underscore, and the name does not contain special characters (such as + 、-、 *,/、.、?、%, &, #等) Example: Define a name of The variable for test (with a value of Managerweb) and a name of installer (a value of publishweb) can do the following:

[Email protected] ~]# Test=manageweb

[Email protected] ~]# Installer=publishweb

3. View and Reference variables:

[Email protected] ~]# echo $test $installer

ManageWeb Publishweb

When the variable name is easily confused with the other characters immediately following it, you need to enclose it with the curly brace "{}", otherwise you will not be able to determine the correct variable name, and the null value will be displayed for undefined variables.

[Email protected] ~]# echo $test 4.5

.5

[Email protected] ~]# echo ${test}4.5

manageweb4.5

4. Special actions for assigning variables

1) Double quotation marks (")

Double quotation marks mainly define the function of the string, especially when the content to be assigned contains spaces, must be enclosed in double quotation marks, in other cases, double quotation marks can often be omitted, for example: if "ManageWeb 6.9" is assigned to the variable test, you should perform "Test=manageweb 6.9"

[Email protected] ~]# Test=manageweb 6.9

-bash:6.9:command not found

[[email protected] ~]# test= "ManageWeb 6.9"

[Email protected] ~]# echo $test

ManageWeb 6.9

Within the double quotation mark range, use the $ symbol to apply the values of other variables, for example:

[Email protected] ~]# type= "ACCP $installer"

[Email protected] ~]# echo $TYPE

ACCP Publishweb

2) Single quotation mark (')

When the content to be assigned contains characters with special meaning such as "$,", \, and so on, because of the use of single quotation marks, in the range of single quotation marks, you will not be able to reference the values of other variables, any word nonspacing as ordinary characters but when the assignment contains single quotation marks, you need to use "\" symbol to escape,

[[email protected] ~]# kill= ' ACCP $installer '//$ symbol cannot apply variable

[[email protected] ~]# echo $kill//output string as-is

ACCP $installer

3) Anti-apostrophe (')

The inverse apostrophe is primarily used for command substitution, which allows the output of a command to be executed to be assigned to a variable, and the range of the backslash must be a command line capable of execution, otherwise an error occurs, for example:

[Email protected] ~]# ls-lh ' which useradd '

-rwxr-x---. 1 root root 101K 2 2011/usr/sbin/useradd

4) Read command

The read command prompts the user to enter information for a simple interactive process that reads a line from the standard input device (keyboard), and assigns the fields read to the specified variable by a space delimiter, assigning the entire row content to the variable if there is only one specified variable. Example: Doing the following will wait for the user to enter text and assign the input value to the variable ToDor1

[email protected] ~]# Read ToDir1

/opt/backup

[Email protected] ~]# echo $ToDir 1

/opt/backup

To make the interface of the interactive operation more user-friendly, the read command can be combined with the "-P" option to set the prompt information.

[Email protected] ~]# read-p "Please specify the backup path:" ToDir2

Please specify the backup Path:/opt/backup

[Email protected] ~]# echo $ToDir 2

/opt/backup

5. Set the scope of the variable:

By default, the newly defined variable is only valid in the current shell environment and is therefore called a local variable. When you enter a child process or a new child shell environment, the local variables are no longer available.

[Email protected] ~]# echo $test $installer

ManageWeb 6.9 Publishweb

[Email protected] ~]# bash

[Email protected] ~]# echo $test $installer


[Email protected] ~]#

To enable user-defined variables to continue to be used in all shell environments and to reduce duplication of effort, the specified variable can be exported as a "global variable" through the internal command export. Cases:

[Email protected] ~]# echo $test $installer

ManageWeb Publishweb

[Email protected] ~]# Export Test installer

[Email protected] ~]# bash

[Email protected] ~]# echo "$test $installer"

ManageWeb Publishweb

Write this today and write the arithmetic of the numeric variable tomorrow. Thank you

This article is from the "one dish can no longer dish of the Little rookie" blog, please be sure to keep this source http://liliming.blog.51cto.com/10925034/1791514

Shell basic Application (ii) use of 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: 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.