為什麼我無法在 bash shell 中建立別名?

為什麼我無法在 bash shell 中建立別名?

在 bash shell 中,為什麼我不能建立別名

$ alias fooo="echo bac"
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

$ alias fooo='echo bac'
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

$ fooo
fooo: command not found

$ alias fooo
bash: alias: fooo: not found

在另一個bash shell中,上述指令成功建立別名

$ alias fooo="echo bac"
$ fooo
bac

在第一個 shell 中,如果我啟動新 shell(只需鍵入bash並按 Enter 鍵),或啟動新的登入 shell(鍵入bash -l),上述命令也會像第二個 shell 中一樣成功。


alias關於第一個shell中命令的回复

$ which alias
$ whereis alias
alias:
$ builtin alias fooo="echo bac"
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'


$ type alias
alias is a shell builtin
$ type -a alias
alias is a shell builtin

$ unalias alias
bash: unalias: alias: not found

echo關於第一個 shell 中的評論

$ echo hello
hello

$ whereis echo
echo: /bin/echo /usr/share/man/man1/echo.1.gz

$ which echo
/bin/echo

答案1

在第一個 shell 上,當您嘗試定義別名時,您會得到包含現有別名的輸出。這是錯誤的,您應該沒有像第二個 shell 那樣的輸出。如果我定義一個名為「alias」的別名,我會重現同樣的問題。

試著找出實際執行的內容,也許只是執行:builtin aliasbuiltin alias foo="echo bar"強制使用別名指令。

答案2

您在第一個 shell 中以這種方式定義了一個函數:

alias(){ builtin alias ; }

type alias應該證實這個假設。

相關內容