bash 在bash中将参数传递给别名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4060880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:49:23  来源:igfitidea点击:

Passing argument to alias in bash

bashshell.bash-profile

提问by slik

Is it possible to do the following:

是否可以执行以下操作:

I want to run the following:

我想运行以下命令:

mongodb bin/mongod

In my bash_profile I have

在我的 bash_profile 我有

alias = "./path/to/mongodb/"

回答by Paused until further notice.

An alias will expand to the string it represents. Anything after the alias will appear after its expansion without needing to be or able to be passed as explicit arguments (e.g. $1).

别名将扩展为它所代表的字符串。别名之后的任何内容都将在其展开后出现,而无需或能够作为显式参数传递(例如$1)。

$ alias foo='/path/to/bar'
$ foo some args

will get expanded to

将扩展到

$ /path/to/bar some args

If you want to use explicit arguments, you'll need to use a function

如果要使用显式参数,则需要使用函数

$ foo () { /path/to/bar "$@" fixed args; }
$ foo abc 123

will be executed as if you had done

将被执行,就好像你已经完成了一样

$ /path/to/bar abc 123 fixed args

To undefine an alias:

要取消定义别名:

unalias foo

To undefine a function:

要取消定义函数:

unset -f foo

To see the type and definition (for each defined alias, keyword, function, builtin or executable file):

要查看类型和定义(对于每个定义的别名、关键字、函数、内置或可执行文件):

type -a foo

Or type only (for the highest precedence occurrence):

或仅键入(对于最高优先级):

type -t foo

回答by leed25d

Usually when I want to pass arguments to an alias in Bash, I use a combination of an alias and a function like this, for instance:

通常,当我想在 Bash 中将参数传递给别名时,我会使用别名和这样的函数的组合,例如:

function __t2d {                                                                
         if [ "x" != 'x' ]; then                                              
            date -d "@"                                                       
         fi                                                                     
} 

alias t2d='__t2d'                                                               

回答by osirisgothra

to use parameters in aliases, i use this method:

要在别名中使用参数,我使用以下方法:

alias myalias='function __myalias() { echo "Hello $*"; unset -f __myalias; }; __myalias'

its a self-destructive function wrapped in an alias, so it pretty much is the best of both worlds, and doesnt take up an extra line(s) in your definitions... which i hate, oh yeah and if you need that return value, you'll have to store it before calling unset, and then return the value using the "return" keyword in that self destructive function there:

它是一个包裹在别名中的自毁函数,所以它几乎是两全其美的,并且不会在您的定义中占用额外的行...我讨厌,哦,是的,如果您需要返回值,您必须在调用 unset 之前存储它,然后在该自毁函数中使用“return”关键字返回值:

alias myalias='function __myalias() { echo "Hello $*"; myresult=$?; unset -f __myalias; return $myresult; }; __myalias'

so..

所以..

you could, if you need to have that variable in there

你可以,如果你需要在那里有那个变量

alias mongodb='function __mongodb() { ./path/to/mongodb/; unset -f __mongodb; }; __mongodb'

of course...

当然...

alias mongodb='./path/to/mongodb/'

would actually do the same thing without the need for parameters, but like i said, if you wanted or needed them for some reason (for example, you needed $2 instead of $1), you would need to use a wrapper like that. If it is bigger than one line you might consider just writing a function outright since it would become more of an eyesore as it grew larger. Functions are great since you get all the perks that functions give (see completion, traps, bind, etc for the goodies that functions can provide, in the bash manpage).

实际上会在不需要参数的情况下做同样的事情,但就像我说的,如果你出于某种原因想要或需要它们(例如,你需要 $2 而不是 $1),你需要使用这样的包装器。如果它大于一行,您可能会考虑直接编写一个函数,因为随着它变大,它会变得更加碍眼。函数很棒,因为您可以获得函数提供的所有好处(请参阅 bash 联机帮助页中的完成、陷阱、绑定等,了解函数可以提供的好处)。

I hope that helps you out :)

我希望能帮到你 :)

回答by blreay

This is the solution which can avoid using function:

这是可以避免使用功能的解决方案:

alias addone='{ num=$(cat -); echo "input: $num"; echo "result:$(($num+1))"; }<<<'

test result

测试结果

addone 200
input: 200
result:201

回答by jimgug

In csh(as opposed to bash) you can do exactly what you want.

csh(而不是bash)中,您可以完全按照自己的意愿行事。

alias print 'lpr \!^ -Pps5'
print memo.txt

The notation \!^causes the argument to be inserted in the command at this point.

该符号\!^导致此时将参数插入命令中。

The !character is preceeded by a \to prevent it being interpreted as a history command.

!字符\前面有a以防止它被解释为历史命令。

You can also pass multiple arguments:

您还可以传递多个参数:

alias print 'lpr \!* -Pps5'
print part1.ps glossary.ps figure.ps

(Examples taken from http://unixhelp.ed.ac.uk/shell/alias_csh2.1.html.)

(示例取自http://unixhelp.ed.ac.uk/shell/alias_csh2.1.html。)

回答by Schroeder

To simplify leed25d's answer, use a combination of an alias and a function. For example:

为了简化 leed25d 的答案,请使用别名和函数的组合。例如:

function __GetIt {
    cp ./path/to/stuff/$* .
}

alias GetIt='__GetIt'