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

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

问题描述

我曾经使用过CShell( csh ) ,使您可以创建一个带参数的别名。表示法类似于

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 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天全站免登陆