码迷,mamicode.com
首页 > 其他好文 > 详细

Bash的几个知识点

时间:2014-09-19 13:39:35      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:ar   for   文件   div   sp   c   amp   ad   r   

1. 区别 builtin command, external command,bash script。
用builtin command(hash、type、command),而不是which命令(external command)查看某个命令是安装了(跟PATH变量相关):
hash foo >/dev/null 2>&  1 || echo "I need the command but it not exist"
或者
command -v foo >/dev/null 2>&  1 || echo "I need the command but it not exist"
或者
type foo >/dev/null 2>&  1 || echo "I need the command but it not exist"
 
2. command执行过程
2.1 builtin command会在bash内部执行,不需要fork子进程
2.2 external command是普通的二进制文件,会fork子进程执行,共享父进程的环境变量。
2.3 bash script:会起一个子进程(exec /bin/bash)解释脚本内容,递归执行内部的external command和bash script。用ps xjf可以查看。
help 命令可以查看所有的builtin command
 
3. A | B
管道会起2个进程,父子进程通过匿名管道进行通信。
 
4. 子shell
( ) 中的指令会在一个子 shell 中执行,命令执行结果不影响当前 shell。
a=hello; echo $a |read var; echo $var
 VS
a=hello; echo $a | (read var; echo $var)
 
5. 优化bash性能几点心得
5.1)少用管道,多用文件作为命令参数。一般的命令都是接受标准输入到标准输出,并介绍文件作为参数的。
5.2)少用管道,多用here文档。read var <<< $(echo "hi"); echo $var   
5.3)少用外部命令,多用builtin command
 
6. 容易混淆的点
6.1)test []  [[]] (())效率是一样的,都是builtin command。可以用help命令查看。

Bash的几个知识点

标签:ar   for   文件   div   sp   c   amp   ad   r   

原文地址:http://www.cnblogs.com/Torstan/p/3981197.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!