1. quoting
shell会自动对通配符(wildcard)和变量(variable)作扩展,还会自动进行命令替换(command substitution),这个特性很方便;可是,有时咱们并不想要这种扩展。
有三种方式能够控制这种扩展。
双引号(double quote):Variable(Yes), Wildcard(NO),Command Substitution(Yes)
单引号(single quote):Variable(NO),Wildcard(NO),Command Substitution(NO)
backslash(\): 在双引号中用backslash能够让$成为一个普通字符,从而使shell不作自动扩展和命令替代。
e.g.
root@localhost :/home/James/mypro/shell# echo $(date)
2012年 05月 10日 星期四 10:53:07 CST
root@localhost :/home/James/mypro/shell# echo "$(date)"
2012年 05月 10日 星期四 10:53:13 CST
root@localhost :/home/James/mypro/shell# echo '$(date)'
$(date)
root@localhost :/home/James/mypro/shell# echo "\$(date)"
$(date)python
2. export语句将某个变量导出到其子进程的环境中。用-n来取消某个变量的“导出”性质。
e.g.
root@localhost :/home/James/mypro/shell# export MYENV="my env"
root@localhost :/home/James/mypro/shell# echo $MYENV
my env
root@localhost :/home/James/mypro/shell# bash
root@localhost :/home/James/mypro/shell# echo $MYENV
my env
root@localhost :/home/James/mypro/shell# localenv="local"
root@localhost :/home/James/mypro/shell# bash
root@localhost :/home/James/mypro/shell# echo $localenvshell
e.g.
root@localhost :/home/James/mypro/shell# export | grep -i myenv
declare -x MYENV="my env"
root@localhost :/home/James/mypro/shell# export -n MYENV
root@localhost :/home/James/mypro/shell# export | grep -i myenvexpress
3. 得到用户输入 -p(输出提示),-t(输入超时)
(和用户交互是很重要的!!!)
e.g.
root@localhost :/home/James/mypro/shell# read -p "Enter Your Name: " name
Enter Your Name: James
root@localhost :/home/James/mypro/shell# echo $name
Jamesbash
4. 数学运算
$((expression))
e.g.
root@localhost :/home/James/mypro/shell# echo $((10 * 5))
50
root@localhost :/home/James/mypro/shell# echo $((10**5))
100000.net
吐槽:其对数学运算的内置支持彻底没有python好啊。之后什么任务要用到数学计算的,能够分离到python脚本里,而后从shell去调用python脚本。进程
5. 获得命令的一些infomation
which command-name
whereis command-name
whatis command-nameget
6. 命令的一些操做
history -- 显示命令历史
Ctrl-r -- 反向查找命令 (比history | grep xxx方便)
!! -- 重复上次命令数学
7. 判断一个string是否包含substring
[[ "$string" =~ "$substring" ]]
string