Shell's operations on variables

Source: Internet
Author: User

One, variable and :- with

Bin= ' DirName "${bash_source-$0}" ' Bin= ' CD "$bin"; PWD ' default_libexec_dir= "$bin"/.. /libexeclibexec_dir= ' #变量set了, but is null libexec_dir=${libexec_dir:-$DEFAULT _libexec_dir}echo $LIBEXEC _dir

 ${PARAMETER:-WORD}

 ${PARAMETER-WORD}

If the parameter PARAMETER is unset (never were defined) or null (empty), this one expands WORD to, otherwise it expands to th E Value of PARAMETER .

If you omit : the (colon), like shown in the second form, the default value is only used when the parameter was UN Set, not if it was empty. For the second case, the parameter variable is not set to use the word value

Second, the role of variable and # #或者percent

 There is a variable foldername=569_20180118150257, if you want to get _ before the 569 or the next 20180118150257, how to do it?

foldername=569_20180118150257 Echo ${foldername##*_} #返回20180118150257 # equivalent to echo ${foldername# #569_} echo ${foldername %%_*} #返回569 # equivalent to echo ${foldername%%_20180118150257}

 From the above can be seen # #或者 percent with the variable is to delete the matching part, # #是从开始匹配569_, and delete 569_,%% is from the back match _*, and delete _*

Note that two # #或者 percent of a percent represents the longest match, with # or% differences to look at the following example:

Filename= "Sequencing.fq.gz" Echo ${filename##*.} #返回gzecho ${filename#*.} #返回fq. GZ Echo ${filename%%.*} #返回sequencingecho ${filename%.*} #返回sequencing. Fq

Third,bash script gets its own path method

Bin= ' DirName "${bash_source[0]}" ' #脚本相对于当前目录的路径, is the relative path bin= ' CD ' $bin "; PWD '

Shell's operations on variables

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.