歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Bash變量擴展修改符

Bash變量擴展修改符

日期:2017/3/1 9:26:48   编辑:Linux編程

Bash變量擴展修改符

1、未設置就臨時替換(:-)

冒號:用來檢驗變量是否設置過,如果沒有冒號,則認為設置過,不替換
$fruit=peach
$echo ${fruit:-plum}
peach

$fruit=
$echo ${fruit:-plum}
plum

$echo $fruit
$

2、未設置就永久替換(:=)

$name=
$echo ${name:=Peter}
Peter

$echo $name
Peter

3、設置了就臨時替換(:+)

$foo=grapes
$echo ${foo:+pears}
pears
$echo $foo
grapes

4、未設置就打印消息(:?)

$echo ${namex:?"namex is undefined"}
namex: namex is undefined

$echo name=${1:?"requires an argument"} //適用於腳本中檢測位置參量
1: requires an argument

echo ${y?}
y: parameter null or not set //打印默認消息

5、子字符串(:m:n)

$var=notebook

$echo ${var:0}
notebook

$echo ${var:0:4}
note

$echo ${var:4:4}
book

Bash函數簡述 http://www.linuxidc.com/Linux/2015-06/118796.htm

Linux Bash腳本編程語言中的美學與哲學 http://www.linuxidc.com/Linux/2015-03/114436.htm

提高Linux工作效率的十大bash技巧 http://www.linuxidc.com/Linux/2015-03/114349.htm

bash的登錄與歡迎信息:/etc/issue,/etc/motd http://www.linuxidc.com/Linux/2014-08/105419.htm

Bash常用的幾個配置文件 http://www.linuxidc.com/Linux/2014-08/105202.htm

Bash腳本15分鐘進階教程 http://www.linuxidc.com/Linux/2014-04/100750.htm

10個 Linux/Unix下 Bash 和 KSH shell 的作業控制實例 http://www.linuxidc.com/Linux/2014-03/98159.htm

Ubuntu下shell腳本運行異常:Bash和dash的區別 http://www.linuxidc.com/Linux/2013-10/91100.htm

Bash腳本之for語句if語句以及各種測試語句 http://www.linuxidc.com/Linux/2013-07/87922.htm

什麼是Bash Shell的內建(build in)命令 http://www.linuxidc.com/Linux/2013-06/86039.htm

分享有用的 bash 別名和函數 http://www.linuxidc.com/Linux/2015-01/111178.htm

Linux Bash Shell入門教程 http://www.linuxidc.com/Linux/2013-08/8848.htm

Copyright © Linux教程網 All Rights Reserved