shell的${}使用

来源:互联网 发布:网络斗牛公式编程原理 编辑:程序博客网 时间:2024/06/06 13:08

${param:-default}   If  param is null, then set it to the value of  default .

${#param} Gives the length of  param
${param%word} From the end, removes the smallest part of  param that
matches  word and returns the rest
${param%%word} From the end, removes the longest part of  param that
matches  word and returns the rest
${param#word} From the beginning, removes the smallest part of  param that
matches  word and returns the rest
${param##word} From the beginning, removes the longest part of  param that
matches  word and returns the rest

=========================================================

unset foo

echo ${foo:-bar}


output:bar

-----------------------------------

foo=fud

echo ${foo:-bar}


output:fud

--------------------------------------

foo=/usr/bin/X11/startx
echo ${foo#*/}

echo ${foo##*/}


output: usr/bin/X11/startx
            startx

--------------------------------------------

bar=/usr/local/etc/local/networks

echo ${bar%local*}

echo ${bar%%local*}


output: /usr/local/etc
            /usr


原创粉丝点击