2014-01-19 31 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) 
相关问题