I want to get the expansion of an alias.
For example, if I have:
alias g=hub
alias cdh='cd $HOME'
I want to have:
expand_alias g
== hub
expand_alias cdh
== cd $HOME
The tricky thing is that the two shells have different output:
bash:
$ alias g cdh
alias g='git'
alias cdh='cd $HOME'
zsh:
% alias g cdh
g=hub
cdh='cd $HOME'
Note no alias
prefix and no quotes around hub
.
Best Answer
In
zsh
, you can just useWith
bash
(assuming it's not in POSIX mode in which case itsalias
would give an output similar tozsh
's), you could do:Basically, we evaluate the output of
alias
after having redefinedalias
as a function that prints what's on the right of the first=
in its first argument.You could use a similar approach for something compatible with most POSIX shells,
zsh
andbash
: