bash 定义引用其他别名的别名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32500481/
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
Define alias that references other aliases
提问by Pensierinmusica
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
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'
. Up to here all fine. Now, where I'm having the problem.
如果我运行type foo
它返回foo is aliased to 'foo'
,并type bar
返回bar is aliased to 'bar'
。到这里一切正常。现在,我遇到了问题。
alias foobar=$foo$bar
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?
有人可以帮忙吗?
回答by anubhava
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 函数来更好地控制它。
回答by glenn Hymanman
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.
里面{}
需要空格和分号。
回答by shivangi aggarwal
I used this in csh and it worked for me :
我在 csh 中使用了它,它对我有用:
alias new_alias 'vi old_alias'
别名 new_alias 'vi old_alias'
回答by Ivan
Here is an example of alias that i'm using
这是我正在使用的别名示例
#clear
alias cls='clear; ls'
alias ccls='cd; cls' # used alias cls