在 CentOS 里配置 alias 报错,常见原因和解决方法如下(按出现频率排序):
现象
$ alias ll='ls -l'
bash: alias: ll: not found
或重启终端后 alias 失效。
原因
解决 根据你使用的 shell:
# 用户级
vi ~/.bashrc
# 添加
alias ll='ls -l'
# 生效
source ~/.bashrc
⚠️ 不要写在
~/.bash_profile里(只对登录 shell 生效)
vi /etc/bashrc
错误示例
alias ll = 'ls -l' # ❌ 等号两边有空格
正确写法
alias ll='ls -l'
错误
alias grep='grep --color=auto''
正确
alias grep='grep --color=auto'
现象
#!/bin/bash
alias ll='ls -l'
ll
报错或无效。
原因 脚本默认是 非交互式 shell,不展开 alias。
解决
shopt -s expand_aliases
source ~/.bashrc
例如:
alias cd='ls'
虽然不报错,但会导致异常行为。
如:
shdash解决
echo $SHELL
确认是 bash 或 zsh
alias
type ll
echo "alias ll='ls -l'" >> ~/.bashrc
source ~/.bashrc
如果你能贴出 具体报错信息 + 你写在哪里,我可以直接帮你定位是哪一条原因。