Linux advanced Variables

Source: Internet
Author: User

Advanced Variables
Basic Form
[1]. variable extension format $ {variable name}
[2]. Command extension format $ (command)
[3]. Arithmetic extended format $ (arithmetic ))
1. variable extension
Basic Format: $ {variable name}
Variable extension meaning: A simple condition judgment that assigns different values of variables to different conditions. It has a great relationship with the existence of variables.
1.1 test the basic usage of Variables
Syntax: $ {variable to be tested-default value}
Note: If the variable to be tested does not exist, the condition is met and the default value is returned for processing.
Resolution: If the variable to be tested does not exist, the default value is returned. Otherwise, the value of the variable is returned.
Instance:
Unset
Echo $ {a-'default'} # output is 'default'
1.2 If the test variable does not exist or its value is null, a default value is returned.
Syntax: $ {variable to be tested:-default value}
Instance:
A =
Echo $ {a:-'default'} # The output is 'default'
1.3 The test variable does not exist or its value is null. assign a default value to the variable.
Syntax: $ {variable to be tested: = default value}
Instance:
A =
Echo $ {a: = 'default '}
Echo $ a #2 rows of output 'default'
1.4 if the test variable does not exist or its value is null, a prompt is displayed.
Syntax: $ {variable to be tested :? Prompt message}
Instance:
A =
Echo r = {:? Prompt information} # output "prompt information"
1.5 if the test variable exists and is not null, the true value is returned.
 
Syntax: $ {variable to be tested: + true value}
Instance:
A = hello
Echo $ {a: + world} # output as "world"
Summary:
Symbol function
: Determines whether the variable is null. If it is null, it is true.
+ Forward: determines whether the variable exists and is not empty. If it exists and is not empty, it is true.
-Reverse: determines whether the variable does not exist. If the variable does not exist, it is null.
= Value assignment,
? Prompt information
2. Take string slices to obtain the string length.
2.1 take string slices
Syntax:
[1]. $ {Variable: Start position index}
Purpose: intercept all characters starting from the specified position and ending with the string. The index of the Start character is 0.
 
Instance:

Str = "hello world"
Echo $ {str: 6} # The output result is "world"

[2]. $ {Variable: Start position: length}
Purpose: intercept a string of the specified length starting from the specified start position.
 
Instance:
Str = "hello world"
Echo $ {str: 6: 2} # output is "wo"
2.2 calculate the string length
Syntax: $ {# variable name}
Instance:
Str = "hello world"
Echo $ {# str} # The output result is 11.
3. Comparison Style
Objective: To extract a part of a variable value (string)
Practice: delete or replace some strings that match the style
3.1 compare and delete the shortest matched items starting from the beginning of the string
Syntax: $ {variable to be tested # style}
Note: The minimum matching style string is deleted from the left side of the variable value to be tested.
Instance:

Filename = "/usr/sbin/ntpdate"
Echo $ {filename #/*/} # display result sbin/ntpdate
3.2 comparison between the longest matched person deleted from the beginning of the string
Syntax: $ {variable to be tested # style}
Instance:
Filename = "/usr/sbin/ntpdate"
Echo $ {filename ###/*/} # Show the result ntpdate
3.3 comparison between the shortest matched items after the string
Syntax: $ {variable to be tested % style}
Instance:
Filename = "/usr/sbin/ntpdate"
Echo $ {filename %/*} # output result/usr/sbin
3.4 Comparison Between the longest matched items after the string
Syntax: $ {variable to be tested % style}
Instance:
Filename = "hello/usr/sbin/ntpdate"
Echo $ {filename %/*} # output result hello

4. Replace or delete some strings
 
4.1 replace only the first string found
Syntax: $ {variable to be tested/style/replacement}
Instance:
Str = "hello"
Echo $ {str/hello/world} # The result is world hello.
4.2 replace all style strings found
Syntax: $ {variable to be tested // style/replace}
Instance:
Str = "hello"
Echo $ {str/hello/world} # The result is world.
4.3 Delete the first specified string.
Syntax: $ {variable to be tested/string to be deleted /}
Instance:
Str = "hello"
Echo $ {str/hello/} # The result is hello.
4.4 delete all specified strings.
Syntax: $ {variable to be tested // variable to be deleted /}
Instance:
Str = "hello world hello"
Echo $ {str // hello/} # The output result is world.
5. Command Extension
Syntax: $ (command)
Instance: time = $ (date) equivalent time = 'date'
6. Arithmetic Extension
Syntax: $ (arithmetic ))
Instance:
Echo "a = $ (5*5)" # The result is a = 25.

 
Author: "huangyandong"

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.