2014-03-12 20 views
1

Zsh是一個非常強大的外殼,但有一段時間,我感到驚訝,因爲它走得太遠了。如何禁用zsh中的內聯函數定義

最近我已經輸入以下命令後,這些令人驚訝的令人費解的時刻之一(適用於簡單和清晰):

% echo "nosuchfunction() nosuchcommand" 
nosuchfunction() nosuchcommand 

% echo nosuchfunction() nosuchcommand 

% echo "nosuchfunction() nosuchcommand" 
echo:3: command not found: nosuchcommand 

% echo I used to like this 
echo:4: command not found: nosuchcommand 

我花了一段時間來了解發生了什麼事情,並認爲這是特定於zsh而不是我嘗試執行的命令。在第2行,我創建了一些內聯函數,並將其分配給「echo」。至少,這是有道理的線3的輸出和4

作爲對比的光的唯一解釋,在bash事情讓多一點點的感覺:

% echo "nosuchfunction() nosuchcommand" 
nosuchfunction() nosuchcommand 

% echo nosuchfunction() nosuchcommand 
bash: syntax error near unexpected token `(' 

% echo "nosuchfunction() nosuchcommand" 
nosuchfunction() nosuchcommand 

% echo I used to like this 
I used to like this 

在這一點上我我也不確定這應該是一個功能還是一個錯誤,但我只想禁用這種行爲。

任何人都知道如何?

回答

3

其實你創建了兩個函數,echonosuchfunction,兩者都執行nosuchcommand

zsh不僅允許「內聯函數定義」(如:如果函數僅包含一個命令,則不需要使用大括號或父項),但也允許爲同一函數提供多個名稱。從zshmisc(1)

function word ... [() ] [ term ] { list } 
word ...() [ term ] { list } 
word ...() [ term ] command 
    where term is one or more newline or ;. Define a function which is 
    referenced by any one of word. Normally, only one word is provided; 
    multiple words are usually only useful for setting traps. The 
    body of the function is the list between the { and }. See the 
    section `Functions'. 

兩種功能(不是錯誤)不是可選的,不能打開的。

最好記住括號是句法元素,在zsh以及bash,如果你不想使用它們,你必須引用它們。 (這不是好像第二行在bash中的工作,所以只是禁用'內聯定義'不會解決任何問題。)