[英]Define alias that references other aliases
I'd need to be able to define an alias in a Debian shell that includes other aliases, for a project I'm currently working on.对于我目前正在处理的项目,我需要能够在包含其他别名的 Debian shell 中定义别名。
Let's look at a code example to make things more clear.让我们看一个代码示例,让事情更清楚。
alias foo=foo
alias bar=bar
If I run type foo
it returns foo is aliased to 'foo'
, and type bar
returns bar is aliased to 'bar'
.如果我运行type foo
它返回foo is aliased to 'foo'
, type bar
返回bar is aliased to 'bar'
。 Up to here all fine.到这里一切都很好。 Now, where I'm having the problem.现在,我遇到了问题。
alias foobar=$foo$bar
Doesn't work.不起作用。 type foobar
returns foobar is aliased to ''
. type foobar
返回foobar is aliased to ''
。
I've tried alias foobar=${foo}${bar}
and it doesn't work either.我试过alias foobar=${foo}${bar}
但它也不起作用。
Once I get this working, in the final version I actually need some text in between the two aliases, imagine something like: alias fooandbar=${foo}and${bar}
.一旦我开始工作,在最终版本中,我实际上需要在两个别名之间添加一些文本,想象一下: alias fooandbar=${foo}and${bar}
。
Can anyone help please?有人可以帮忙吗?
To reuse alias in another alias use:要在另一个别名中重用别名,请使用:
foobar='foo;bar'
However I would suggest you to consider using shell function to get better control over this.但是我建议您考虑使用 shell 函数来更好地控制它。
Following @anubhava's sage advice:遵循@anubhava 的明智建议:
foo() { echo foo; }
bar() { echo bar; }
foobar() { echo "$(foo)$(bar)"; }
fooandbar() { echo "$(foo)and$(bar)"; }
Spaces and semicolons inside {}
are required there. {}
内需要空格和分号。
I used this in csh and it worked for me :我在 csh 中使用了它,它对我有用:
alias new_alias 'vi old_alias'别名 new_alias 'vi old_alias'
Here is an example of alias that i'm using这是我正在使用的别名示例
#clear
alias cls='clear; ls'
alias ccls='cd; cls' # used alias cls
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.