制作一个带参数的 Bash 别名? [英] Make a Bash alias that takes a parameter?

查看:33
本文介绍了制作一个带参数的 Bash 别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经使用 CShell (),它允许您创建一个带有参数的别名.符号类似于

I used to use CShell (csh), which lets you make an alias that takes a parameter. The notation was something like

alias junk="mv \!* ~/.Trash"

在 Bash 中,这似乎不起作用.鉴于 Bash 有许多有用的功能,我认为这个功能已经实现,但我想知道如何实现.

In Bash, this does not seem to work. Given that Bash has a multitude of useful features, I would assume that this one has been implemented but I am wondering how.

推荐答案

Bash 别名不直接接受参数.您必须创建一个函数.

Bash alias does not directly accept parameters. You will have to create a function.

alias 不接受参数,但可以像别名一样调用函数.例如:

alias does not accept parameters but a function can be called just like an alias. For example:

myfunction() {
    #do things with parameters like $1 such as
    mv "$1" "$1.bak"
    cp "$2" "$1"
}


myfunction old.conf new.conf #calls `myfunction`

顺便说一下,在您的 .bashrc 和其他文件中定义的 Bash 函数可以作为 shell 中的命令使用.因此,例如您可以像这样调用较早的函数

By the way, Bash functions defined in your .bashrc and other files are available as commands within your shell. So for instance you can call the earlier function like this

$ myfunction original.conf my.conf

这篇关于制作一个带参数的 Bash 别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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