Search code examples
bashpipenvmkdocs

Launch bash subshell and run commands in a script


I want to write a shell script that does the following:

  1. Activate pipenv virtual environment
  2. 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'
  1. Pipenv allows you to run a command in the virtual environment without launching a shell:
    $ pipenv run <insert command here>
    
  2. bash -c <insert command here> allows you to pass a command to bash to execute
    $ bash -c "echo hello"
    hello
    
  3. 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.

Solution

  • You can use this command :

    pipenv run bash -c 'mkdocs serve ; exec bash --norc'