2017-08-09 132 views
1

這裏是我的.zshrc巖組不是來自於函數識別別名

alias inst="adb install -r -d" 

notify() { 
    start=$(date +%s) 
    "[email protected]" 
    notify-send "\"$(echo [email protected])\" took $(($(date +%s) - start)) seconds to finish" 
} 

當我運行命令notify inst app.apk,我得到一個錯誤:

notify:2: command not found: inst 
    0.00s user 0.00s system 0% cpu 0.001 total 

任何人都能夠解釋爲什麼這並未一些輕」噸的工作,並希望有辦法使其工作?

回答

0

不要使用別名腳本

首先,根據the advanced bash scripting guide

In a script, aliases have very limited usefulness.

所以,你可以考慮不使用別名,但在同一頁面,例如功能(當然,2款低):

Almost invariably, whatever we would like an alias to do could be accomplished much more effectively with a function.

甲哈克溶液

如果這是爲了使用.zshrc中的別名而爲自己製作一個快速腳本,那麼仍然有一個出路。

alias foo='echo hello' 

bar() { 
    `alias "[email protected]" | cut -d\' -f2` 
} 

bar foo # => hello 

別名替換

alias man page

The first word of each simple command, if unquoted, is checked to see if it has an alias. If so, that word is replaced by the text of the alias. The alias name and the replacement text can contain any valid shell input, including shell metacharacters, with the exception that the alias name can not contain `='.

The first word of the replacement text is tested for aliases, but a word that is identical to an alias being expanded is not expanded a second time. This means that one can alias ls to "ls -F", for instance, and Bash does not try to recursively expand the replacement text.

Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt .

The rules concerning the definition and use of aliases are somewhat confusing. Bash always reads at least one complete line of input before executing any of the commands on that line. Aliases are expanded when a command is read, not when it is executed. Therefore, an alias definition appearing on the same line as another command does not take effect until the next line of input is read. The commands following the alias definition on that line are not affected by the new alias. This behavior is also an issue when functions are executed. Aliases are expanded when a function definition is read, not when the function is executed, because a function definition is itself a compound command. As a consequence, aliases defined in a function are not available until after that function is executed. To be safe, always put alias definitions on a separate line, and do not use alias in compound commands.

+0

問題是關於zsh,而不是bash。由於zsh是bash的一個特性超集,因此您在答案中引用bash文檔時會錯過一些內容。 – JoshuaRLi

+0

這對我來說仍然相當準確。你在我的引用中找到不適用於'zsh'的東西嗎?儘管如此,我仍然看到了你的解決方案,並且喜歡它的全球化別名,它確實使用了zsh是超集的事實!但要回答OP,仍然使用正常的別名,我仍然認爲我的答案是準確的 –

1

當shell處理命令,除其他事項外(例如PATH搜索),它會檢查,查看是否第一令牌/參數(空格分隔)屬於在當前環境中加載的別名。如果您嘗試替換的別名不是第一個標記,則替換不會發生。如果您的別名碰巧不是PATH或當前目錄上的可執行文件的名稱,則該錯誤將傳回至command not found

由於您的問題是關於Z Shell,zsh實際上提供了一個鮮爲人知的功能,稱爲全局別名。如果使用-g標誌聲明別名,則zsh不僅會對第一個標記進行適當替換,而且還會對令牌進行適當替換,而不管其順序如何。

alias -g inst="adb install -r -d"應該做的伎倆。

請記住,這是一個zsh僅配備於便攜性的原因,並確保任何腳本你的寫作有一個調用zsh殼shebang行:#!/usr/bin/env zsh

我也建議不要使用zsh全球重要或生產腳本中出現鋸齒。對於個人使用,這是完全沒問題的。