I want to alias cd
so that it takes me to the root of my current git project, and if that can't be found it takes me to my normal home dir.
I am trying to set HOME
to either the git root or, if that can't be found, my normal home variable.
alias cd='HOME="${$(git rev-parse --show-toplevel):-~}" cd'
It doesn't work though.
You can't run a command inside
${}
, except in the fallback clause for when a value is not set (in POSIX sh or bash; might be feasible in zsh, which allows all manner of oddball syntax).Regardless, far fewer contortions are needed if using a function:
Note:
command cd
is used to call through to the realcd
implementation rather than recursing.