TransWikia.com

Define cd so that it changes a variable (as it does already with PWD, OLDPWD, ...)

Code Review Asked on November 21, 2021

My target is to have an environment variable being set to a leading substring of $PWD and to be unset when $PWD does not start with that substring.

So I decided to override cd like this (here I’m assuming the substring is just my home directory /home/enrico, but in general is fine for me that it be an hardcoded regex):

cd() {
    command cd "$@"
    export MYVARIABLE=$(<<< $PWD sed -E 's!(/home/enrico)(/?$|.*)!1!')
    if [[ $MYVARIABLE == $PWD ]]; then
        unset MYVARIABLE
    fi
}

This seems to work fine, but I’d like to have some different point of view on it.

(Btw, if there are appropriate tags to add, please do, as I haven’t found any more than bash.)

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP