Define alias that references other aliases

你说的曾经没有我的故事 提交于 2020-08-22 04:34:31

问题


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.

Let's look at a code example to make things more clear.

  1. alias foo=foo
  2. 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.

  1. alias foobar=$foo$bar

Doesn't work. type foobar returns foobar is aliased to ''.

I've tried alias foobar=${foo}${bar} and it doesn't work either.

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}.

Can anyone help please?


回答1:


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.




回答2:


Following @anubhava's sage advice:

foo() { echo foo; }
bar() { echo bar; }
foobar() { echo "$(foo)$(bar)"; }
fooandbar() { echo "$(foo)and$(bar)"; }

Spaces and semicolons inside {} are required there.




回答3:


I used this in csh and it worked for me :

alias new_alias 'vi old_alias'




回答4:


Here is an example of alias that i'm using

#clear
alias cls='clear; ls'
alias ccls='cd; cls' # used alias cls


来源:https://stackoverflow.com/questions/32500481/define-alias-that-references-other-aliases

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!