home ~ projects ~ socials

Check If The Python venv deactivate Function Exists From The Command Line

Basic Usage

Run this on the command line to check if a function like Python's virtual environment deactivate exists:

if  $(type deactivate &> /dev/null 2>&1) 
  then
    echo "Found it"
  else 
    echo "It is not there"
  fi
Output:
It is not there

I use this in setup scripts to see if I need to deactivate an existing virtual env before creating a new one (e.g. in the cdk).

#+NOTES:

These are some of the notes I took while figuring this out. They need to be cleaned up. I'm leaving them here for reference in the mean time.

I tried running the examples from this page: https://stackoverflow.com/questions/592620/how-can-i-check-if-a-program-exists-from-a-bash-script

but command and hash don't work for me with the deactivate function.

The return values from when the virtual environment was activated were:

#+begin_example

command: 1 type: 0 hash: 1

#+end_example

and when it was off

#+begin_example

command: 1 type: 1 hash: 1

#+end_example

so, type was the only one that responded properly

this one: https://stackoverflow.com/questions/85880/determine-if-a-function-exists-in-bash

shows using type -t which is available in /bin/bash on a mac running osX 12.x but not in /bin/zsh

Also, running this:

bash -c "type -t deactivate"

- I tried the three command here, but they didn't work for me: https://stackoverflow.com/questions/592620/how-can-i-check-if-a-program-exists-from-a-bash-script

-- end of line --