在 CentOS(以及所有 Linux 发行版)里,alias 本身是不支持直接传参的。
alias 只是 文本替换,例如:
alias ll='ls -l'
你执行:
ll /home
实际变成:
ls -l /home
✅ 参数只能 放在 alias 后面
❌ 不能在 alias 内部使用 $1、$2 这样的位置参数
比如下面这样是 不行的:
alias test='echo $1' # ❌ 无效
如果你需要“传参”,应该用 shell 函数:
mygrep() {
grep "$1" file.txt
}
使用:
mygrep hello
甚至支持多个参数:
copy() {
cp "$1" "$2"
}
copy a.txt b.txt
把 alias 或函数写到:
~/.bashrc
然后执行:
source ~/.bashrc
| 方式 | 是否支持传参 |
|---|---|
| alias | ❌ 不支持 |
| shell 函数 | ✅ 支持 |
如果你有具体使用场景(比如 ssh、docker、git),我可以帮你写一个合适的函数。