Linux Shell programming parameters and variable extensions

Source: Internet
Author: User

Linux Shell programming parameter and variable extension linux shell parameter extension $ {param} extends the value of the variable, and prevents the variable from being parsed incorrectly, such: $ {hello} world, $ {arr [1] }$ {arr [*]}, or $ {arr [@]}: returns all elements of the array arr $ {! You can use $ {! In param} bash 2 {! Param} for indirect reference, such as $ {! #} Returns the last location parameter $ {! Arr [*]} or $ {! Arr [@]}: returns all subscripts of array arr to delete matched substrings $ {param # pattern}: deletes matched substrings starting with param (minimum match) $ {param # pattern}: Delete the child string matching pattern from the beginning of param (maximum match) $ {param % pattern}: Delete the child string matching pattern from the end of param (minimum match) $ {param % pattern}: removes the matched substring (maximum match) from the end of param to extract the substring $ {param: offset }: extract the substring starting from position offset in param (offset starts from 0 ). If offset is a negative number, it indicates that the offset starts from the end of the string. The offset must be enclosed in parentheses, or a space before offset $ {param: offset: length }: extract the length of the Child string starting from the offset in param. Other substrings that are similar to the same as param can be * or @, which indicates sharding of the location parameter, for example, $ {@:} returns the value of $ {var-string} for the two location parameters starting with the 3rd Location Parameter Based on the variable status. If var is not defined, the return value is string, otherwise, return the value of var $ {var:-string}: If var is undefined or empty, return the string; otherwise, return the value of var $ {var = string}: If var is undefined, assign string to var and return the value of var. Otherwise, the value of var $ {var: = string} is directly returned. If var is undefined or empty, the string is assigned. Return the value of var. Otherwise, return the value of var $ {var + string}. If var has been defined, return the value of string. Otherwise, return the value of var $ {var: + string}: If var is not empty, string is returned. Otherwise, the var value $ {var? is returned? String}: If var is not defined, the string is output to the standard error and the script is exited. Otherwise, the var value $ {var:? is returned :? String}: If var is undefined or empty, the string is output to the standard error and the script is exited. Otherwise, the return var value mode replaces $ {param/pattern/string }: replace the first substring In param that matches pattern with string $ {param // pattern/string }: replace all substrings In param that match the pattern with string $ {param/# pattern/string}. If param matches the pattern at the beginning, replace the start with string $ {param/% pattern/string}. If the end of param matches pattern, replace the end with string. If param is @ or *, replace the pattern of all location parameters. If param is set to arr [@] Or arr [*], replace the length and number of variables for all elements in the array in the pattern $ {# param}: Return Length of the param value $ {#*} and $ {#@}: returns the number of location parameters $ {arr [*]} and $ {arr [@]}: returns the number of elements in the array. The variable is case-insensitive. $ {param ^ pattern}: pattern is a single character, convert the first character In param that matches pattern to uppercase $ {param ^ pattern}: pattern to a single character, and convert all characters matching pattern in param to uppercase $ {param, pattern}: pattern is a single character. Convert the first character In param that matches pattern to lower case $ {param, pattern}: pattern to a single character, convert all characters in param that match the pattern to lowercase. param can be *, @, arr [*], or arr [@]. If the pattern is null, it is treated ?, Match any character

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.