在函数内部执行传递的别名? [英] Execute a passed alias inside a function?

查看:53
本文介绍了在函数内部执行传递的别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

我正在尝试使函数能够按设定的间隔运行命令,因为我无法访问监视"程序.从我最想写的功能简化为: runit(){$ 1;} .

I'm trying make a function that runs commands on a set interval because I don't have access to a "watch" program. Simplified to it's most basic from, the function I'm trying to write is runit() { $1; }.

有效的方法:

当我将非别名的内容传递给它时,此方法既正常又花哨.例如, runit"ls -l" 可以正常工作.我从 ls -l <​​/code>命令获得了完整的输出.

This works fine and dandy when I pass it things that aren't aliases. For example, runit "ls -l" works fine. I get the full output from the ls -l command.

什么不起作用:

当我给它传递别名时,问题就开始了.例如,设置 alias ll ="ls -l" 然后调用 runit"ll" 将导致 -bash:ll:找不到命令

The problem starts when I pass it an alias. For example, setting alias ll="ls -l" then calling runit "ll" will result in -bash: ll: command not found.

我尝试过的事情:

当我硬编码别名 runit(){ll;} ,它可以正常工作,并且给了我期望的结果.

When I hard-code the alias runit() { ll; }, it works fine and gives me what I expect.

我觉得我可能正在俯视某些东西,但是我不能完全将手指放在上面.
为什么对别名进行硬编码会很好,但是将其传递给函数会失败?
有没有办法完成我想做的事情?

I feel like I might be overlooking something, but I can't quite place my finger on it.
Why would hard-coding the alias work fine, but passing it into the function fail?
Is there a way to accomplish what I'm attempting to do?

推荐答案

bash 手册页上讨论了别名(我的重点是):

From the bash man page discussion of aliases (emphases mine):

别名在读取命令时扩展,而不是在执行时扩展.因此,别名定义与另一个命令出现在同一行上,直到读取下一行输入后,该定义才生效.这命令该行上别名定义后面的内容不受新别名的影响.当函数是被执行.在读取函数定义时(而不是在执行函数时)会扩展别名,因为函数定义是本身就是一个复合命令.结果,在函数中定义的别名只有在执行该函数之后才可用.至安全起见,请始终将别名定义放在单独的行上,并且不要在复合命令中使用别名.

Aliases are expanded when a command is read, not when it is executed. Therefore, an alias definition appearing on the same line as another command does not take effect until the next line of input is read. The commands following the alias definition on that line are not affected by the new alias. This behavior is also an issue when functions are executed. Aliases are expanded when a function definition is read, not when the function is executed, because a function definition is itself a compound command. As a consequence, aliases defined in a function are not available until after that function is executed. To be safe, always put alias definitions on a separate line, and do not use alias in compound commands.

您可以通过使用type命令在函数中观察到这种效果:

You can observe this effect in functions by using the type command:

$ run_it () { ll; }
$ type run_it

您应该看到函数的主体包含对 ls -l <​​/code>的调用,而不是对 ll 的调用.

You should see that the body of the function contains a call to ls -l, not ll.

别名部分的最后一句话:

The last sentence of the section on aliases:

几乎所有目的,别名都被shell函数取代.

For almost every purpose, aliases are superseded by shell functions.

我对该行的解释是:如果您认为要使用别名,请尝试首先编写一个函数.除非该函数明显无法完成您需要的操作,否则不要使用别名.

My interpretation of that line is: if you think you want to use an alias, try writing a function first. Don't use an alias unless the function demonstrably fails to do what you need.

这篇关于在函数内部执行传递的别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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