2014-01-19 30 views
1

我的bash的版本是:爲什麼我的bash版本不接受函數關鍵字?

bash --version 
GNU bash, version 4.2.45(1)-release (x86_64-pc-linux-gnu) 

如果我做的原型功能

#!/bin/bash 
function f() 
{ 
echo "hello" $1 
} 
f "world" 

我得到Syntax error: "(" unexpected

這是爲什麼?禁用了javascript的

輸出是:

autocd   off 
cdable_vars  off 
cdspell   off 
checkhash  off 
checkjobs  off 
checkwinsize on 
cmdhist   on 
compat31  off 
compat32  off 
compat40  off 
compat41  off 
direxpand  off 
dirspell  off 
dotglob   off 
execfail  off 
expand_aliases on 
extdebug  off 
extglob   on 
extquote  on 
failglob  off 
force_fignore on 
globstar  off 
gnu_errfmt  off 
histappend  on 
histreedit  off 
histverify  off 
hostcomplete off 
huponexit  off 
interactive_comments on 
lastpipe  off 
lithist   off 
login_shell  off 
mailwarn  off 
no_empty_cmd_completion off 
nocaseglob  off 
nocasematch  off 
nullglob  off 
progcomp  on 
promptvars  on 
restricted_shell off 
shift_verbose off 
sourcepath  on 
xpg_echo  off 
+0

作品我..確保沒有特殊,隱藏,文件中的字符 – hek2mgl

+0

奇怪。有關如何查找這些角色的任何提示? – fpghost

+0

試試'cat -A filename'。它將控制字符擴展爲可打印的形式,並在每行的末尾添加一個「$」。 –

回答

5

你的bash確實版本接受function關鍵字。問題是你沒有在bash下運行你的腳本。

我得到同樣的錯誤,如果我運行dash foo.bashsh foo.bash腳本(/bin/sh是一個符號鏈接dash我的系統上)。

dash不識別function語法。

#!/bin/bash行使您的腳本被bash解釋 - 但只有當你直接調用它時,而不是如果你將它餵給其他shell。

而不是調用殼通過你的腳本名稱,將其作爲參數:

只需直接調用腳本:

foo.bash (or ./foo.bash) 
相關問題