如何在 bash 别名中包含参数? [英] How do I include parameters in a bash alias?

查看:41
本文介绍了如何在 bash 别名中包含参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试创建:

alias mcd="mkdir $1; cd $1"

获取:

$ mcd foo
usage: mkdir [-pv] [-m mode] directory ...
-bash: foo: command not found

我做错了什么?

推荐答案

别名只能用任意文本替换命令的第一个单词.不能使用参数.

An alias can only substitute the first word of a command with some arbitrary text. It can not use parameters.

您可以改为使用 shell 函数:

You can instead use a shell function:

mcd()
{
  test -e "$1" || mkdir "$1"
  cd "$1"
}

这篇关于如何在 bash 别名中包含参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆