The bash variable for Linux

Source: Internet
Author: User

Advanced parts of Bash variables

1. Variable expansion

${variable name} variable extension for example: ${filename}

$ (command) command replacement for example: $ (LS/)

$ (arithmetic) calculation extension for example: $ ((9+9))

Variable extension: A simple conditional judgment, the substitution value that assigns different variables to different conditions, is an implicit "if-then-else" program syntax

Basic syntax for judging whether a variable exists or not: ${the variable to be tested-default value}

Formula: There is a transmission exists, no, the default value is passed

Presence or absence or null: ${variable to be tested-: Default value}

If there is a pass, no or empty, the default value is passed in

Presence or absence or null: ${variables to be tested: = default}

Formulas: Set default values for variables that do not exist or are empty

Presence or absence or NULL, output hint: ${variables to be tested:? Prompt message}

Purpose: In the script, in order to detect the variable is empty or not present, output prompt information to prevent serious problems in the script

Testing the existence of variables: ${variables to be measured: + truth}

Purpose: To test the existence, existence and value of a variable, which is true

To organize your pictures, please see attachments

When a variable is a string, the use of the slice substring:

There are roughly two kinds: 1.${variable: Position start} 2.${variable: Position start: Length}

Take the length of the passed-in string: ${#变量名称} The extension of this method applies: ${#数组 [@]} or ${#数组 [*]} are the number of array elements taken

Style comparison application: ${variable # style} Starting from the far left, contrasting variable values, deleting "shortest match string"

${variable # #样式} ... "Best-fit string"

${variable% style} Starting from the far right, contrasting variable values, deleting "shortest match string"

${variable-percent-style} .... "Best-fit string"

Replace or delete a partial string

${variable/style/substitution string}: Replaces only the first matched string

${variable//style/replacement string}: Replace all matching strings

${variable/style}: Delete the first matching string

${variable//style}: Delete all matching strings

The bash variable for Linux

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.