在 Ubuntu 中,aliases 不生效是一个非常常见的问题,通常不是写错了,而是加载时机或作用范围不对。下面按最常见原因逐一说明。
~/.bashrc 没被加载Ubuntu 默认:
~/.bashrc~/.bashrc✅ 解决方式:
# 编辑 ~/.bashrc
nano ~/.bashrc
# 在最后加上
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
并确认 ~/.bash_profile 或 ~/.profile 中有:
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
然后:
source ~/.bashrc
如果你用的是:
那么 ~/.bashrc 里的 alias 完全不会生效
✅ 检查当前 shell:
echo $SHELL
写在:
~/.zshrc
alias ll 'ls -al'
funcsave ll
常见错误位置:
/etc/profile(只对登录 shell)~/.profile(不总是加载)~/.bashrc~/.bash_aliases(需被 bashrc 引用)例如:
alias ll='ls -l'
后面又有:
alias ll='ls'
✅ 检查是否已存在:
alias ll
在脚本中 alias 默认不展开
✅ 必须加:
shopt -s expand_aliases
source ~/.bashrc
⚠️ 但一般不建议在脚本中用 alias
修改后必须执行:
source ~/.bashrc
或重新打开终端
echo "alias hi='echo hello'" >> ~/.bashrc
source ~/.bashrc
hi
| 现象 | 原因 |
|---|---|
| SSH 登录不生效 | bashrc 未加载 |
| 新终端不生效 | 写错文件 |
| zsh/fish 不生效 | shell 不对 |
| 脚本中不生效 | alias 不展开 |
| 有时生效有时不 | 被覆盖 |
如果你愿意,可以把:
发出来,我可以直接帮你定位是哪一种。