Hi everyone I'm learning how to use the .bashrc file in linux and as my title states I'm wondering how to make a function recognize an argument as an alias
I have an alias called home defined as: alias home=$HOME
and a function go defined as
function go(){
cd $1
ls $1
}
but when I do go home
i get
bash: cd: home: No such file or directory
ls: cannot access home: No such file or directory
when I want it to do go $HOME
how would i go about implementing this?
An alias is not a word substitution but a small newly created command:
$ alias bla=ls
$ bla
file1
file2
file3
…
So, it cannot be used in the way you assumed.
You might want to use variable substitution for this:
$ home=$HOME
$ function go() {
cd "$(eval echo \$"$1")"
}
$ go home
In case you want to use an alias despite that this is an abuse, try this:
$ alias home=$HOME
$ function go() {
cd "$(type "$1" | sed -e 's/.*is aliased to .//' -e 's/.$//')"
}
$ go home
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With