sh xxx.sh報錯「Syntax error: "(" unexpected」html
sh +x xxx.sh也未出現詳細信息shell
os version:debian 8.5express
出現以上錯誤時,通常有如下幾種可能:數組
一、第一行不是#!/bin/bashbash
二、腳本亂碼函數
三、語法錯誤ui
檢查了腳本,排除了以上可能,最後嘗試給腳本加上x權限,./xxx.sh卻能正常執行,bash xxx.sh及bash -x xxx.sh都能預期執行,由此發現sh連接到了/bin/dash,而不是/bin/bashspa
bash - GNU Bourne-Again SHell
dash - Debian Almquist Shell操作系統
------------------------------------------------------htm
將debian的sh連接修改成習慣的bash,有如下兩種方法
一、ln -s /bin/bash /bin/sh
二、sudo dpkg-reconfigure dash
----------------------------------------------------
dash與bash的區別
摘自:https://zhidao.baidu.com/question/489742760031792892.html
Linux 操做系統缺省的 shell 是Bourne Again shell,它是 Bourne shell 的擴展,簡稱 Bash,與 Bourne shell 徹底向後兼容,而且在Bourne shell 的基礎上增長、加強了不少特性。
GNU/Linux 操做系統中的 /bin/sh 本是 bash (Bourne-Again Shell) 的符號連接,但鑑於 bash 過於複雜,有人把 ash 從 NetBSD 移植到 Linux 並改名爲 dash (Debian Almquist Shell),並建議將 /bin/sh 指向它,以得到更快的腳本執行速度。Dash Shell 比 Bash Shell 小的多,符合POSIX標準。
Debian和Ubuntu中,/bin/sh默認已經指向dash,這是一個不一樣於bash的shell,它主要是爲了執行腳本而出現,而不是交互,它速度更快,但功能相比bash要少不少,語法嚴格遵照POSIX標準。
語法上的主要的區別有: 1.定義函數 bash: function在bash中爲關鍵字 dash: dash中沒有function這個關鍵字 2.select var in list; do command; done bash:支持 dash:不支持, 替代方法:採用while+read+case來實現 3. echo {0..10} bash:支持{n..m}展開 dash:不支持,替代方法, 採用seq外部命令 4. here string bash:支持here string dash:不支持, 替代方法:可採用here documents 5. >&word重定向標準輸出和標準錯誤 bash: 當word爲非數字時,>&word變成重定向標準錯誤和標準輸出到文件word dash: >&word, word不支持非數字, 替代方法: >word 2>&1; 常見用法 >/dev/null 2>&1 6. 數組 bash: 支持數組, bash4支持關聯數組 dash: 不支持數組,替代方法, 採用變量名+序號來實現相似的效果 7. 子字符串擴展 bash: 支持${parameter:offset:length},${parameter:offset} dash: 不支持, 替代方法:採用expr或cut外部命令代替 8. 大小寫轉換 bash: 支持${parameter^pattern},${parameter^^pattern},${parameter,pattern},${parameter,,pattern} dash: 不支持,替代方法:採用tr/sed/awk等外部命令轉換 9. 進程替換<(command), >(command) bash: 支持進程替換 dash: 不支持, 替代方法, 經過臨時文件中轉 10. [ string1 = string2 ] 和 [ string1 == string2 ] bash: 支持二者 dash: 只支持= 11. [[ 增強版test bash: 支持[[ ]], 可實現正則匹配等強大功能 dash: 不支持[[ ]], 替代方法,採用外部命令 12. for (( expr1 ; expr2 ; expr3 )) ; do list ; done bash: 支持C語言格式的for循環 dash: 不支持該格式的for, 替代方法,用while+$((expression))實現 13. let命令和((expression)) bash: 有內置命令let, 也支持((expression))方式 dash: 不支持,替代方法,採用$((expression))或者外部命令作計算 14. $((expression)) bash: 支持id++,id--,++id,--id這樣到表達式 dash: 不支持++,--, 替代方法:id+=1,id-=1, id=id+1,id=id-1