一、变量和:-连用

bin=`dirname "${BASH_SOURCE-$0}"`
bin=`cd "$bin"; pwd` DEFAULT_LIBEXEC_DIR="$bin"/../libexec
LIBEXEC_DIR=''#变量set了,但是是空值
LIBEXEC_DIR=${LIBEXEC_DIR:-$DEFAULT_LIBEXEC_DIR}
echo $LIBEXEC_DIR

 $ {PARAMETER:-WORD}

 $ {PARAMETER-WORD}

 If the parameter PARAMETER is unset (never was defined) or null (empty), this one expands to WORD, otherwise it expands to the value of PARAMETER.

 If you omit the : (colon), like shown in the second form, the default value is only used when the parameter was unset, not when it was empty.针对第二种情况,PARAMETER变量未set,才使用WORD值

二、变量和##或者%%连用的作用

 有一变量foldername=569_20180118150257,如果想获取_前的569或者后面的20180118150257,该如何操作呢?

foldername=569_20180118150257

echo ${foldername##*_}
#返回20180118150257
#等价与
echo ${foldername##569_} echo ${foldername%%_*}
#返回569
#等价与echo ${foldername%%_20180118150257}

 从上可以看出##或者%%与变量连用是要删除匹配的部分,##是从开始匹配569_,并删除569_,%%是从后面匹配_*,并删除_*

 注意,两个##或者%%表示的是最长匹配,与#或者%区别看下面的例子:

filename="sequencing.fq.gz"
echo ${filename##*.}#返回gz
echo ${filename#*.}#返回fq.gz echo ${filename%%.*}#返回sequencing
echo ${filename%.*}#返回sequencing.fq

三、Bash脚本获取自身路径方法

bin=`dirname "${BASH_SOURCE[0]}"` #脚本相对于当前目录的路径,是相对路径
bin=`cd "$bin"; pwd`
05-11 19:21