Deletion, substitution and substitution of shell variable contents

Source: Internet
Author: User

Deletion of shell variables:

[Email protected]:~$ Path=${path} # #设置path和PATH相同内容 [email protected]:~$ echo $path/usr/local/sbin:/usr/local/bin:/ Usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games[email protected]:~$ echo ${path#/*sbin:} # #加上 ' # ' symbol delete Match '/* Sbin: ' Minimum combination/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games[email protected]:~$ echo ${ Path##/*sbin:} # #加上 ' # # ' delete match '/*sbin: ' Maximum combination/bin:/usr/games:/usr/local/games

Variable Setting method

Description

${variable # keyword}

Delete the shortest combination if the variable content starts with the data in the keyword

${variable # #关键字}

Delete the longest combination if the variable content starts with the "keyword" data

${Variable% keyword}

Delete the shortest combination if the variable content starts at the end of the data that matches the keyword

${Variable% keyword}

Delete the longest combination if the variable content starts at the end of the data that matches the keyword

${variable/old string/new string}

Replace the first old string with a new string if the contents of the variable conform to the old string

${variable//old string//new String}

Replace the first old string with a new string if the contents of the variable conform to the old string

Substitution of shell variables:

[Email protected]:~$ Path=${path} # #设置path和PATH相同内容 [email protected]:~$ echo $path/usr/local/sbin:/usr/local/bin:/ Usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games[email protected]:~$ Echo ${path/sbin/newsbin} # # Replace the first sbin with a newsbin/usr/local/newsbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games[ Email protected]:~$ Echo ${path//sbin//newsbin} # #将所有的sbin替换成newsbin/usr/local//newsbin:/usr/local/bin:/usr// Newsbin:/usr/bin://newsbin:/bin:/usr/games:/usr/local/games

Substitution of shell variables:

How variables are set
Str not set
STR is empty
STR contains content
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= not change var= str= unchanged var= $str
VAR=${STR:=EXPR} str=expr var=expr str=expr var=expr str= unchanged 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





Deletion, substitution and substitution of shell 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.