Pensierinmusica
Pensierinmusica

Reputation: 6950

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.

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?

Upvotes: 25

Views: 21896

Answers (4)

Ivan
Ivan

Reputation: 7277

Here is an example of alias that i'm using

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

Upvotes: 2

shivangi aggarwal
shivangi aggarwal

Reputation: 1

I used this in csh and it worked for me :

alias new_alias 'vi old_alias'

Upvotes: -1

glenn jackman
glenn jackman

Reputation: 246807

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.

Upvotes: 7

anubhava
anubhava

Reputation: 785128

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.

Upvotes: 37

Related Questions