1

Goal: Automatically execute bash commands if when in directory.

For example, if I enter a git project directory, I'd like bash to run the following for me:

  • conda activate
  • export VAR_NAME=foo

I attempted by appending to ~/.bashrc, but with no luck:

...
if [ -f "/home/me/PycharmProjects/project/" ]; then
    conda activate project_venv
    export KEY=foo
    export SECRET=bar
fi
DanielBell99
  • 896
  • 5
  • 25
  • 57
  • Personally, I implemented a system that uses the `zsh` hook for `chpwd` (directory change) that checks for specific folders within each directory with scripts to be run. Bash doesn't have that hook, but others have shared implementations, like [this one](https://stackoverflow.com/a/57121539/570918), that could be used to similar effect. – merv Aug 12 '22 at 16:03
  • 2
    Don't run your env_vars.sh. It will be run in a subshell environment and your exports cannot affect the calling shell's environment. Source it instead: `. ~/.../env_vars.sh` – M. Nejat Aydin Aug 12 '22 at 16:07
  • Note: Git is not relevant here (git-bash doesn't have anything to do with Git, if that's how you got there) and the [tag:export] tag is about exporting data, not bash's `export` built-in. – torek Aug 13 '22 at 01:04

3 Answers3

3

You can set PROMPT_COMMAND, see the bash docs. Bash executes its value (or if it's an array, each of its values) before every prompt, so you can do whatever you want when PWD changes.

jthill
  • 55,082
  • 5
  • 77
  • 137
2

You can add this function to your ~/.bashrc:

cd () { 
    command cd "$@" &&
    if [[ $(pwd) = '/home/me/PycharmProjects/project' ]]; then
        conda activate project_venv
        export KEY=foo SECRET=bar
    fi
}
M. Nejat Aydin
  • 9,597
  • 1
  • 7
  • 17
1

Because you are exporting in a function you need to use declare -gx declare --help will give you the best and most accurate reason why but it is because all function vars are technically local. The -g create a global exported var for a function is ignored if not in a function and the -x is what export is an alias for. export is just declare -x. You will also need to source your script files

So it will look like this

declare -gx KEY=foo
declare -gx SECRET=bar
cd () {
    command cd "$@" &&
    if [[ $(pwd) = '/home/me/PycharmProjects/project1' ]]; then
        conda activate project1
        source ~/miniconda3/etc/activate.d/env_vars.sh
    elif [[ $(pwd) = '/home/me/PycharmProjects/project2' ]]; then
        conda activate project2
    else
       source ~/miniconda3/etc/deactivate.d/env_vars.sh
    fi
}

Full disclosure I'm not sure if the -x is completely necessary but I do it in case of sourcing a script.

Also storing in secrets in ~/.bashrc is a general no no as it leads to bad actors getting secrets. Ontop of slowing down your interactive shell loading times

  • 1
    _"Because you are exporting in a function you need to use `declare -gx`"_: There isn't such a need. Using the `export` builtin command is fine in a function. – M. Nejat Aydin Aug 15 '22 at 11:47