I want to write a shell script that does the following:
- Activate pipenv virtual environment
- Runs
mkdocs serve
which starts a local dev server for my mkdocs documentation
If I do the naïve thing and put this in my script:
cd <my-docs-directory>
pipenv shell
mkdocs serve
it fails because pipenv shell
"launches a subshell in the virtual environment". I need to pass the mkdocs serve
command into the virtual shell (and preferably land in that same shell after running the script ).
Thanks in advance!
Answer
Philippe's answer works. Here's why.
pipenv run bash -c 'mkdocs serve ; exec bash --norc'
- Pipenv allows you to run a command in the virtual environment without launching a shell:
$ pipenv run <insert command here>
bash -c <insert command here>
allows you to pass a command to bash to execute$ bash -c "echo hello" hello
exec
serves to replace current shell process with a command, so that parent goes a way and child owns pid. Here's a related question on AskUbuntu.