在 Linux 的 shell alias(别名)里,不能直接带参数。
alias 只是做文本替换,例如:
alias ll='ls -l'
执行 ll 就相当于执行 ls -l。
但下面这种是不行的:
alias grep='grep --color=auto $1' # ❌ 不支持 $1
alias 不会解析参数位置。
如果你需要“带参数”,应该用 shell 函数:
grep() {
command grep --color=auto "$@"
}
或者:
mkcd() {
mkdir -p "$1" && cd "$1"
}
把这段放进 ~/.bashrc 或 ~/.zshrc 即可。
alias 只能把参数放在后面:
alias g='git'
g status # 实际是 git status
这对简单场景够用,但复杂逻辑必须用函数。
$1 $2如果你有具体使用场景,我可以帮你改写成函数。