http://www.cppblog.com/prayer/archive/2009/05/27/85884.html
{str:=expr}
如果變量str不爲空,${str:=expr}就等於str的值,若str爲空,就把expr的值賦值給str。
http://codingstandards.iteye.com/blog/1160298
用途說明
我們知道,在Linux系統中,冒號(:)常用來做路徑的分隔符(PATH),數據字段的分隔符(/etc/passwd)等。其實,冒號(:)在Bash中也是一個內建命令,它啥也不做,是個空命令、只起到佔一個位置的作用,但有時候確實需要它。當然,它也有它的用途的,否則沒必要存在。在·Linux的幫助頁中說它除了參數擴展和重定向之外不產生任何作用。
No effect; the command does nothing beyond expanding arguments and performing any specified redirections. A zero exit code is returned.
常用參數
格式::
·啥也不做,只起到佔位符的作用。比如在編寫腳本的過程中,某些語法結構需要多個部分組成,但開始階段並沒有想好或完成相應的代碼,這時就可以用:來做佔位符,否則執行時就會報錯。
- if [ "today" == "2011-08-29" ]; then
- :
- else
- :
- fi
格式:: your comment here
格式:# your comment here
寫代碼註釋(單行註釋)。
格式:: 'comment line1
comment line2
more comments'
寫多行註釋。
格式:: >file
格式:>file
清空文件file的內容。
格式:: ${VAR:=DEFAULT}
當變量VAR沒有聲明或者爲NULL時,將VAR設置爲默認值DEFAULT。如果不在前面加上:命令,那麼就會把${VAR:=DEFAULT}本身當做一個命令來執行,報錯是肯定的。
使用示例
示例一 參數擴展
[root@node56 ~]# : abc=1234
[root@node56 ~]# echo $abc
[root@node56 ~]# : ${abc:=1234}
[root@node56 ~]# echo $abc
1234
[root@node56 ~]# ${abc:=1234}
-bash: 1234: command not found
[root@node56 ~]#
示例二 清空文件
[root@node56 ~]# cat <<<"Hello" >123.txt
[root@node56 ~]# cat 123.txt
Hello
[root@node56 ~]# : >123.txt
[root@node56 ~]# cat 123.txt
[root@node56 ~]#
示例三 腳本註釋、佔位符
腳本test_colon.sh
- #!/bin/sh
- : this is single line comment
- : 'this is a multiline comment,
- second line
- end of comments'
- if [ "1" == "1" ]; then
- echo "yes"
- else
- :
- fi
[root@node56 ~]# ./test_colon.sh
yes
http://asadman.blog.hexun.com/56958072_d.html
-bash-3.2$ : echo "this is a comment" > word
-bash-3.2$ ls
MakeFileFindtree word
makefile中的冒號有很多用處:
1.賦值的時候用;
2.“:目標依賴關係
3.還用於分隔不同的目錄,如:
SRC_PATH
?= .:..
風格1: 遞歸擴展變量
(recursively expanded variable)
變量定義格式是,變量和值之間用等號,即 =
例如:
foo = $(bar)
bar = $(ugh)
ugh = Huh?
all:;echo $(foo)
將顯示 Huh?
再例如:
CFLAGS = $(include_dirs) -O
include_dirs = -Ifoo -Ibar
缺點是不能這麼定義:CFLAGS = $(CFLAGS) -O ,將會死循環
風格2: 簡單擴展變量
(simply expanded variables)
變量定義格式是,變量和值之間用冒號等號,即 :=
例如
x := foo
y := $(x) bar
x := later
等價於:
y := foo bar
x := later
另外 ?= 含義爲:沒有定義則賦值
FOO ?= bar
等價於
ifeq ($(origin FOO), undefined)
FOO = bar
endif
+= 是爲變量後面追加字符
變量替換
$(var:a=b),是將 var 變量中每一個單詞後面的 a 替換爲 b
$(var:suffix=replacement)
等價於
$(patsubst %suffix,%replacement,$(var))
$(foo:%.o=%.c) ,由於出現了 %, 其功能和 patsubst 等價
$(var:pattern=replacement)
等價於
$(patsubst pattern,replacement,$(var))
風格1: 遞歸擴展變量
(recursively expanded variable)
變量定義格式是,變量和值之間用等號,即 =
例如:
foo = $(bar)
bar = $(ugh)
ugh = Huh?
all:;echo $(foo)
將顯示 Huh?
再例如:
CFLAGS = $(include_dirs) -O
include_dirs = -Ifoo -Ibar
缺點是不能這麼定義:CFLAGS = $(CFLAGS) -O ,將會死循環
風格2: 簡單擴展變量
(simply expanded variables)
變量定義格式是,變量和值之間用冒號等號,即 :=
例如
x := foo
y := $(x) bar
x := later
等價於:
y := foo bar
x := later
另外 ?= 含義爲:沒有定義則賦值
FOO ?= bar
等價於
ifeq ($(origin FOO), undefined)
FOO = bar
endif
+= 是爲變量後面追加字符
變量替換
$(var:a=b),是將 var 變量中每一個單詞後面的 a 替換爲 b
$(var:suffix=replacement)
等價於
$(patsubst %suffix,%replacement,$(var))
$(foo:%.o=%.c) ,由於出現了 %, 其功能和 patsubst 等價
$(var:pattern=replacement)
等價於
$(patsubst pattern,replacement,$(var))
風格1: 遞歸擴展變量
(recursively expanded variable)
變量定義格式是,變量和值之間用等號,即 =
例如:
foo = $(bar)
bar = $(ugh)
ugh = Huh?
all:;echo $(foo)
將顯示 Huh?
再例如:
CFLAGS = $(include_dirs) -O
include_dirs = -Ifoo -Ibar
缺點是不能這麼定義:CFLAGS = $(CFLAGS) -O ,將會死循環
風格2: 簡單擴展變量
(simply expanded variables)
變量定義格式是,變量和值之間用冒號等號,即 :=
例如
x := foo
y := $(x) bar
x := later
等價於:
y := foo bar
x := later
另外 ?= 含義爲:沒有定義則賦值
FOO ?= bar
等價於
ifeq ($(origin FOO), undefined)
FOO = bar
endif
+= 是爲變量後面追加字符
變量替換
$(var:a=b),是將 var 變量中每一個單詞後面的 a 替換爲 b
$(var:suffix=replacement)
等價於
$(patsubst %suffix,%replacement,$(var))
$(foo:%.o=%.c) ,由於出現了 %, 其功能和 patsubst 等價
$(var:pattern=replacement)
等價於
$(patsubst pattern,replacement,$(var))
將顯示 Huh?
再例如:
CFLAGS = $(include_dirs) -O
include_dirs = -Ifoo -Ibar
缺點是不能這麼定義:CFLAGS = $(CFLAGS) -O ,將會死循環
風格2: 簡單擴展變量
(simply expanded variables)
變量定義格式是,變量和值之間用冒號等號,即 :=
例如
x := foo
y := $(x) bar
x := later
等價於:
y := foo bar
x := later
另外 ?= 含義爲:沒有定義則賦值
FOO ?= bar
等價於
ifeq ($(origin FOO), undefined)
FOO = bar
endif
+= 是爲變量後面追加字符
變量替換
$(var:a=b),是將 var 變量中每一個單詞後面的 a 替換爲 b
$(var:suffix=replacement)
等價於
$(patsubst %suffix,%replacement,$(var))
$(foo:%.o=%.c) ,由於出現了 %, 其功能和 patsubst 等價
$(var:pattern=replacement)
等價於
$(patsubst pattern,replacement,$(var))
風格2: 簡單擴展變量
(simply expanded variables)
變量定義格式是,變量和值之間用冒號等號,即 :=
例如
x := foo
y := $(x) bar
x := later
等價於:
y := foo bar
x := later
另外 ?= 含義爲:沒有定義則賦值
FOO ?= bar
等價於
ifeq ($(origin FOO), undefined)
FOO = bar
endif
+= 是爲變量後面追加字符
變量替換
$(var:a=b),是將 var 變量中每一個單詞後面的 a 替換爲 b
$(var:suffix=replacement)
等價於
$(patsubst %suffix,%replacement,$(var))
$(foo:%.o=%.c) ,由於出現了 %, 其功能和 patsubst 等價
$(var:pattern=replacement)
等價於
$(patsubst pattern,replacement,$(var))
變量替換
$(var:a=b),是將 var 變量中每一個單詞後面的 a 替換爲 b
$(var:suffix=replacement)
等價於
$(patsubst %suffix,%replacement,$(var))
$(foo:%.o=%.c) ,由於出現了 %, 其功能和 patsubst 等價
$(var:pattern=replacement)
等價於
$(patsubst pattern,replacement,$(var))
$(var:pattern=replacement)
等價於
$(patsubst pattern,replacement,$(var))
關於第一種用法:
Makefile裏的=顯然是賦值的用法