Discussion:
[Help-bash] Firing a shell function when I change directories
Maxim Kupfer
2018-12-07 15:19:47 UTC
Permalink
Hi all,

I'm trying to automate the activation of my python virtual environment so
that whenever I cd into a directory with a virtual environment, it
automatically activates. I can already create a function that checks if the
directory venv is available (and activate if it is), but I can't figure out
how to make this run after I enter a new directory. Hoping I could get some
help from this mailing list.

Thanks.
Greg Wooledge
2018-12-07 15:42:58 UTC
Permalink
Post by Maxim Kupfer
I'm trying to automate the activation of my python virtual environment so
that whenever I cd into a directory with a virtual environment, it
automatically activates. I can already create a function that checks if the
directory venv is available (and activate if it is), but I can't figure out
how to make this run after I enter a new directory. Hoping I could get some
help from this mailing list.
cd() {
builtin cd "$@" || return
if [[ -d venv ]]; then
source whatever
fi
}
Maxim Kupfer
2018-12-07 16:23:22 UTC
Permalink
Great, this is very helpful.

As a follow up, I have two different venv directories with their own
activation script paths depending on how the virtual environment was
created. To accommodate both, I used:
source script 1 || source script 2

This is throwing an error though when script 2 should be run, it says: No
such file or directory, but the second script runs fine. How do I make this
silent so that it doesn't give an error?

Thanks, and sorry if this is considered an unrelated follow up.
Post by Greg Wooledge
Post by Maxim Kupfer
I'm trying to automate the activation of my python virtual environment so
that whenever I cd into a directory with a virtual environment, it
automatically activates. I can already create a function that checks if
the
Post by Maxim Kupfer
directory venv is available (and activate if it is), but I can't figure
out
Post by Maxim Kupfer
how to make this run after I enter a new directory. Hoping I could get
some
Post by Maxim Kupfer
help from this mailing list.
cd() {
if [[ -d venv ]]; then
source whatever
fi
}
Greg Wooledge
2018-12-07 16:29:30 UTC
Permalink
Post by Maxim Kupfer
source script 1 || source script 2
This is throwing an error though when script 2 should be run, it says: No
such file or directory, but the second script runs fine. How do I make this
silent so that it doesn't give an error?
source script1 2>/dev/null || source script2
Maxim Kupfer
2018-12-07 16:35:13 UTC
Permalink
Works like a charm. Thanks!
Post by Greg Wooledge
Post by Maxim Kupfer
source script 1 || source script 2
This is throwing an error though when script 2 should be run, it says: No
such file or directory, but the second script runs fine. How do I make
this
Post by Maxim Kupfer
silent so that it doesn't give an error?
source script1 2>/dev/null || source script2
Chet Ramey
2018-12-07 15:45:14 UTC
Permalink
Post by Maxim Kupfer
Hi all,
I'm trying to automate the activation of my python virtual environment so
that whenever I cd into a directory with a virtual environment, it
automatically activates. I can already create a function that checks if the
directory venv is available (and activate if it is), but I can't figure out
how to make this run after I enter a new directory. Hoping I could get some
help from this mailing list.
Use a `cd' shell function. There are several examples in the bash
distribution.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU ***@case.edu http://tiswww.cwru.edu/~chet/
Loading...