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

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

Define alias that references other aliases

linuxbashshellunix

提问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.

让我们看一个代码示例,让事情更清楚。

  1. alias foo=foo
  2. alias bar=bar
  1. alias foo=foo
  2. alias bar=bar

If I run type fooit returns foo is aliased to 'foo', and type barreturns 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'。到这里一切正常。现在,我遇到了问题。

  1. alias foobar=$foo$bar
  1. alias foobar=$foo$bar

Doesn't work. type foobarreturns 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