8

Let's say I have the following alias definition in my ZSH config file:

alias myalias="cd /"

how can I use myalias inside another alias? ie:

alias myaliasone="myalias && cd /usr"

I have tried (with a real alias this one is a fake just for example purposes) but nothing happens and I am not able to see any errors as per debug or fix something, any ideas? If this is the right way to use alias inside another alias, do you know how I can debug this so I can find the root problem or what is happening?

1

1 Answer 1

10

There are two ways to do it:

Use a function instead of an alias

myalias() {
  cd /
}
alias myaliasone='myalias && cd /usr'

Use the $aliases table

zmodload -F zsh/parameter p:aliases
alias myalias='cd /'
alias myaliasone='$aliases[myalias] && cd /usr'

Note that the string needs to be in 'single quotes', to prevent $parameters inside it from being substituted immediately. Now they will be substituted when the command line is being evaluated, after aliases are substituted.

2
  • I know this is a bit dated now but ... how do you call others aliases inside your alias function? Let's say you have the docker-compose plugin enabled and I want to call dcup inside my alias... I did it as (this goes inside my function) cd /usr || exit && dcup but it does not execute the 2nd command in the chain meaning dcup never gets executed
    – ReynierPM
    Commented Mar 30, 2022 at 11:26
  • 1
    @ReynierPM If cd /usr succeeds (returns 0), then whatever is to the right of || won’t be evaluated. See zsh.sourceforge.io/Doc/Release/… Commented Apr 11, 2022 at 14:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .