1.等號兩邊不能有空格,例如:數組
獲取七天前的日期:函數
before_7_day=`date -d "7 days ago" +%Y-%m-%d`
2.自定義函數只能返回數值,不能返回數組、字符串等內容。若是須要生成字符串等結果,能夠聲明爲全局變量,而後將值賦給全局變量,這樣就能夠在函數外訪問ui
get_string(){ number1=1 number2=2 return $number1-$number2 }
返回錯誤:spa
return: 1-2: numeric argument required
修改:code
number get_string(){ number1=1 number2=2 number=$number1-$number2 }
3.字符串拼接不用「+」blog
例如:字符串
若是使用「+」:get
name1="chun" name2="tian" echo $name1+$name2
輸出爲:string
chun+tian
修改成:for循環
name1="chun" name2="tian" echo ${name1}${name2}
輸出:
chuntian
4. for循環使用
5.${ }、#、##、%、%%使用範例
假設定義了一個變量爲,【代碼以下】:
file=/dir1/dir2/dir3/my.file.txt
能夠用${ }分別替換獲得不一樣的值:
${file#*/}:刪掉第一個 / 及其左邊的字符串:dir1/dir2/dir3/my.file.txt
${file##*/}:刪掉最後一個 / 及其左邊的字符串:my.file.txt
${file#*.}:刪掉第一個 . 及其左邊的字符串:file.txt
${file##*.}:刪掉最後一個 . 及其左邊的字符串:txt
${file%/*}:刪掉最後一個 / 及其右邊的字符串:/dir1/dir2/dir3
${file%%/*}:刪掉第一個 / 及其右邊的字符串:(空值)
${file%.*}:刪掉最後一個 . 及其右邊的字符串:/dir1/dir2/dir3/my.file
${file%%.*}:刪掉第一個 . 及其右邊的字符串:/dir1/dir2/dir3/my
【記憶的方法爲】:
# 是 去掉左邊(鍵盤上#在 $ 的左邊)
%是去掉右邊(鍵盤上% 在$ 的右邊)
單一符號是最小匹配;兩個符號是最大匹配
${file:0:5}:提取最左邊的 5 個字節:/dir1
${file:5:5}:提取第 5 個字節右邊的連續5個字節:/dir2
也能夠對變量值裏的字符串做替換:
${file/dir/path}:將第一個dir 替換爲path:/path1/dir2/dir3/my.file.txt
${file//dir/path}:將所有dir 替換爲 path:/path1/path2/path3/my.file.txt