*1 echo $ var1=100 $ echo $var1 $ echo "hello world"
hp@ubuntu:~$ fruit=apple
hp@ubuntu:~$ echo "have ${fruit}(s)"
have apple(s)
hp@ubuntu:~$ echo -n "hello" #忽略換行
hellohp@ubuntu:~$
hp@ubuntu:~$ echo -e "he\tllo" #轉移
he llo
*2 printf
hp@ubuntu:~$ printf "%s %s %-5s %.2f\n" no name mark 77.564
no name mark 77.56
腳本語言通常不需要在使用變量之前聲明其類型,隻需要直接指派就可以!在bash中,每個變量都是字元串。
*3 環境變量
$ cat /proc/$pid/environ # 檢視某程序運作時的環境變量
$ pgrep gedit # 獲得該程序的程序id
$ cat /proc/4067/environ | tr '\0' '\n'
一些shell可用的常見的環境變量 home, pwd, user, uid, shell
*4 補充内容
hp@ubuntu:~$ echo ${#var1} #字元串長度
3
算術運算 let, (()), [] 執行基本的算術操作。
expr 和 bc 這兩個工具也會非常有用。
$ let result=no1+no2 $ let no1++ $ let no1--
$ echo $result
9
result=$[ no1+no2 ] result=$[ $no1+5 ] result=$(( no1+50 ))
expr同樣可以用于基本算術運算 result='expr 3+4' result=$(expr $no1+5)
echo "4 * 0.56" | bc
2.24
*5 檔案描述符和重定向
檔案描述符是與檔案輸入,輸出想關聯的整數。它們用來跟蹤已打開的檔案。
最常見的檔案描述符 0-stdin, 1-stdout, 2-stderr
$ echo "this is world" > temp.txt $ echo "this is world2" >> temp.txt
$ cmd 2> stderr 1> stdout.txt
/dev/null 是一個特殊的裝置檔案-黑洞。
........................