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