Linux Advanced variables

Source: Internet
Author: User
Tags first string

Advanced variables
Basic form
[1]. variable extension format ${variable name}
[2]. command extension Format $ (command)
[3]. Arithmetic extension format $ (arithmetic)
1. Variable expansion
Basic form: ${variable name}
Variable extension meaning: a simple conditional judgment that assigns different values to different conditions. It has a large relationship with the existence or otherwise of a variable.
1.1 Basic usage of test variables for existence
Syntax: ${variables to be tested-default value}
Note: If the variable to be measured does not exist, then it conforms to the judging condition, and returns the default value as the disposition.
Parse: Returns the default value if the variable being tested does not exist, otherwise returns the value of the variable
Instance:
Unset A
echo ${a-' default ' #输出为 ' default '
1.2 A default value is passed back when the test variable does not exist or its value is empty
Syntax: ${variables to be tested:-Default value}
Instance:
A=
echo ${a:-' default ' #输出为 ' default '
1.3 The test variable does not exist or its value is empty to assign a default value to the variable
Syntax: ${variables to be tested: = default value}
Instance:
A=
echo ${a:= ' default '}
echo $a #输出为2行 ' default '
1.4 Test variable does not exist or its value is empty, output a hint message
Syntax: ${variables to be tested:? Prompt message}
Instance:
A=
echo r={a: Prompt message} #输出 "Tips"
1.5 The test variable exists and is non-null returns the truth value

Syntax: ${variables to be tested: + truth}
Instance:
A=hello
Echo ${a:+world} #输出为 "World"
Summarize:
Symbolic effect
: Determines whether the variable is empty and true if it is empty
+ Forward, determine if the variable exists and is not empty, true if it exists and is not empty
-Reverse, determine if the variable does not exist, or null if it does not exist
= assigned value,
? Prompt information
2. Take a string slice to get the string length
2.1 Taking a string slice
Grammar:
[1].${variable: The starting position index}
Function: intercepts all characters starting at the specified position to the end of the string, with the starting character indexed to 0

Instance:

str= "Hello World"
Echo ${str:6} #输出结果为 "World"

[2].${variable: Start position: Length}
Function: Intercepts a string of the specified length starting at the specified starting position.

Instance:
str= "Hello World"
Echo ${str:6:2} #输出为 "Wo"
2.2 Calculating string Lengths
Syntax: ${#变量名称}
Instance:
str= "Hello World"
echo ${#str} #输出结果为11
3. Contrast style
Purpose: To intercept a part of a variable value (string)
Procedure: Delete or replace a partial string that conforms to the style
3.1 Starting from the front of the string delete the shortest match
Syntax: ${variables to be measured # style}
Description: Deletes the shortest matching style string from the left of the value of the variable to be measured
Instance:

Filename= "/usr/sbin/ntpdate"
Echo ${filename#} #显示结果ntpdate
3.3 Starting from behind the string delete the shortest match
Syntax: ${the variable to be measured% style}
Instance:
Filename= "/usr/sbin/ntpdate"
Echo ${filename%/*} #输出结果/usr/sbin
3.4 Delete the longest match by starting from behind the string
Syntax: ${the variable to be measured in percent style}
Instance:
Filename= "Hello/usr/sbin/ntpdate"
Echo ${filename%%/*} #输出结果hello

4. Replace or delete a partial string

4.1 Replace only the first string found
Syntax: ${variables to be tested/style/replace}
Instance:
str= "Hello hello"
Echo ${str/hello/world} #结果为world Hello
4.2 Replace all found style strings
Syntax: ${variables to be tested//style/replace}
Instance:
str= "Hello hello"
Echo ${str/hello/world} #结果为world World
4.3 Delete the first specified string
Syntax: ${the variable to be tested/the string to delete/}
Instance:
str= "Hello hello"
Echo ${str/hello/} #结果为hello
4.4 Delete all the specified strings
Syntax: ${variables to be tested//variables to be deleted/}
Instance:
str= "Hello World Hello"
Echo ${str//hello/} #输出结果为world
5. Command Extensions
Syntax: $ (command)
Example: time=$ (date) equivalent time= ' Date '
6. Arithmetic expansion
Syntax: $ ((arithmetic))
Instance:
echo "a=$ ((5*5))" #结果为a =25

Linux Advanced variables

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.