`

我使用过的Linux命令之:(冒号) - 啥也不做(除了……)

阅读更多

我使用过的Linux命令之:(冒号) - 啥也不做(除了……)

本文链接:http://codingstandards.iteye.com/blog/1160298   (转载请注明出处)

 

用途说明

我们知道,在Linux系统中,冒号(:)常用来做路径的分隔符(PATH),数据字段的分隔符(/etc/passwd)等。其实,冒号(:)在Bash中也是一个内建命令,它啥也不做,是个空命令、只起到占一个位置的作用,但有时候确实需要它。当然,它也有它的用途的,否则没必要存在。在·Linux的帮助页中说它除了参数扩展和重定向之外不产生任何作用。

 

man : 写道
: [arguments]
    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
[root@node56 ~]#

 

问题思考

相关资料

【1】Kpld’s Blog  Kpld’s Blog
http://kpld8888.wordpress.com/
【2】nighteblis.cublog.cn  bash中的冒号, 以及bash bomb解析还有各种各样的炸*弹
http://www.cublog.cn/u1/40976/showart_2528550.html
【3】清水如画的博客  2011-03-20 bash之特殊符号冒号、大括号扩展应用、代码块
http://blog.sina.com.cn/s/blog_4002e0d20100qh4w.html

 

 

返回 我使用过的Linux命令系列总目录

 

8
3
分享到:
评论
2 楼 codingstandards 2011-08-31  
山风小子 写道
不错~~~

谢谢支持
1 楼 山风小子 2011-08-30  
不错~~~

相关推荐

Global site tag (gtag.js) - Google Analytics