Shell and Shellscript Learning Note III (deletion, substitution and substitution of variable contents)

Source: Internet
Author: User

1 . Deletion and substitution of variable contents

# The representation is deleted from the front, so here it starts / deletes ,* instead of 0 to infinitely more than any character

% Delete variable contents from behind

For example:echo ${path%:* bin} deletes the most a directory, that is, the string from: to the bin and is the shortest

Echo ${path/sbin/sbin}: two slashes in the middle of the old character, followed by the new character

How variables are set

Description

${ variable # keyword }

If the data from the beginning of the variable content matches the "keyword", the shortest data is deleted

${ variable # # keyword }

If the data from the beginning of the variable content matches the "keyword", the shortest data is deleted

${ variable % keyword }

If the variable content from the tail forward data conforms to the "keyword", then the minimum data to be met is deleted

${ variable % keyword }

The longest data to be met is deleted if the contents of the variable content from the tail forward data conform to the "keyword"

${ variable / old string / new string }

If the contents of the variable conform to the old string, the first old string is replaced by the new string

${ variable // old string / new string }

If the contents of the variable conform to the old string, all old strings are replaced by the new string

2. Testing of variables and content substitution

How variables are set

Str is not set

Str is an empty string

STR has set a non-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 does not change
Var=

Str does not change
Var= $str

VAR=${STR:=EXPR}

str=expr
var=expr

str=expr
var=expr

Str does not change
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 and Shellscript Learning Note III (deletion, substitution and substitution of variable contents)

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.