Access, deletion, substitution and substitution of shell variables

Source: Internet
Author: User

<< Bird's private cuisine >>

Access to variables: Echo

Echo $variable
Echo $PATH
Echo ${path}

Configuration rules for variables
1. Variables and variable contents are linked by an equal sign "=" as follows:
"Myname=vbird"

2. The equal sign cannot be directly connected to the space character, as shown below as an error:
"myname = Vbird" or "Myname=vbird Tsai"

3. The variable name can only be an English letter and a number, but the beginning character cannot be a number, the following is an error:
"2myname=vbird"

4. 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
The special characters within the double quotation mark such as $ etc., can retain the original characteristics, as follows:
"var=" Lang is $LANG "" will be "Lang is en_US"
The special characters in single quotation marks are only ordinary characters (plain text), as follows:
"var= ' lang is \ $LANG '" will be "Lang is \ $LANG"

5. Use the caret character "\" To turn special symbols (such as [Enter], $, \, space, ' etc.) into general characters;

6. 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"

7. If the variable is the content of the amplified variable, the contents can be summed up with the "\$ variable" or the \${variable}, as follows:
"Path=" $PATH ":/home/bin"

PS. I generally use the \${variable}, in the variable splicing is particularly important, name=\ $nameyes, the name of the content is \ $nameyes this variable; name=\${name}yes, you can combine \ $name and yes

8. If the variable needs to be run in another subroutine, you need to export the variable into an environment variable:
"Export PATH"

9. Usually uppercase characters are system default variables, self-configuring variables can use lowercase characters, easy to judge (purely according to user interests and hobbies);

10. To cancel a variable, use unset: "unset variable name", for example, to cancel the configuration of myname:
"Unset myname"

Deletion, substitution, and substitution of variable contents

Example one: The lowercase path custom variable is configured to be the same as the path content
Path=${path}
Echo $path
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

Example two: Suppose you want to delete the first two directories, how to display them?

echo ${path#/*kerberos/bin:}
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

Explain:

Example three: I want to delete all the previous directories, leaving only the last one

echo ${path##/*:}
/root/bin

Add a # to # # and turn it into a "delete the longest data"

    • #: The "shortest" one that conforms to the substituted text;
    • # #: The "longest" fit for the substituted text

What if you want to "delete the contents of a variable from behind"? This time you have to use the percentage (%) Symbol.

Example four: I want to delete the last directory, that is, the string from: to the bin

echo ${path%:* Bin}
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

Example five: What if I only want to keep the first directory?

echo ${path%%:* Bin}
/usr/kerberos/sbin

Now that you've learned the removal feature, let's talk about replacing it.

Example six: Replace the sbin in the variable contents of path with the uppercase Sbin:
Echo ${path/sbin/sbin}
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
This part is much easier to understand! The key word is the two slashes, the middle of the two slashes is the old string, followed by the new string

Echo ${path//sbin/sbin}
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
If it is a two slash, then everything that fits will be replaced.

Summarize

How variables are configured Description
${variable # keyword}
${variable # #关键词}
If the data from the beginning of the variable content matches the "keyword", the shortest data is deleted
If the contents of the variable content start from scratch conform to "keyword", the longest data will be deleted
${Variable% keyword}
${variable percent percent keyword}
If the variable content from the tail forward data conforms to the "keyword", then the minimum data will be met to delete
The longest data to be met is deleted if the contents of the variable match the "keyword" data from the tail forward.
${variable/old string/new string}
${variable//old string/new string}
If the contents of the variable conform to "old string" then "the first old string will be replaced by a new string"
If the contents of the variable conform to "old string" then "all old strings will be replaced by new strings"

Testing of variables and content substitution

At some point we often need to "judge" the existence of a variable, if the variable exists to use the existing configuration, if the variable does not exist, give a common configuration.

Example one: Test if there is a username variable, and if not present, give username content as root
Echo $username
<== because there is a blank, username may not exist, or it may be an empty string
Username=${username-root}
Echo $username
Root <== because username is not configured, it actively gives content named Root.
Username= "Vbird Tsai" <== active configuration username content
Username=${username-root}
Echo $username
Vbird Tsai <== because username has been configured, use the old configuration instead of root

Example two: If username is not configured or is an empty string, the username content is configured as root
Username= ""
Username=${username-root}
Echo $username
<== because username is configured as an empty string! So of course it's reserved as an empty string!
Username=${username:-root}
Echo $username
Root <== Plus ":" After the contents of the variable is empty or is not configured, can be replaced with the following content!

How variables are configured STR Not Configured Str is an empty string STR is configured not to be an empty string
VAR=${STR-EXPR} var=expr Var= Var= $str
VAR=${STR:-EXPR} var=expr var=expr Var= $str
VAR=${STR+EXPR} Var= var=expr var=expr
VAR=${STR:+EXPR} Var= Var= var=expr
VAR=${STR=EXPR} str=expr
var=expr
STR does not change
Var=
STR does not change
Var= $str
VAR=${STR:=EXPR} str=expr
var=expr
str=expr
var=expr
STR does not change
Var= $str
VAR=${STR?EXPR} Expr output to stderr Var= Var= $str
VAR=${STR:?EXPR} Expr output to stderr Expr output to stderr Var= $str

Access, deletion, substitution and substitution 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.