Access, deletion, substitution and substitution of "Shell" variables

Source: Internet
Author: User

--from "Brother Bird's Linux private dish"

--summarize and make it convenient for inspection

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 amplification variable, then the "\$ variable" or \${variable} can be used to accumulate the content, as follows:
"Path=" $PATH ":/home/bin"

PS. I generally use \${variable}, in the variable splicing is particularly important, name=\$nameyes, name is the contents of \$nameyes this variable; name=\${ Name}yes, the \$name And yes can be stitched together

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 }
${ variable }

If the data from the beginning of the variable content matches the "keyword", the longest data to be met is deleted
${ variable }
${ variable }

${ 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 ":" If the contents of the variable are empty or not configured, Can be replaced with a later 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 unchanged
var=
str invariant
var= $str
var=${str:=expr} str=expr
var=expr
str=expr
var=expr
Span style= "Font-family:arial, Helvetica, Sans-serif; font-size:15px; " >str invariant
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

Shell variable fetch, delete, replace, and replace

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.