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