diegus
diegus

Reputation: 1188

Run bash command that affect the parent shell

I have this bash script that is suppose to activate a virtualenv in the current directory, cd into another folder and execute a command, i.e. export a pythonpath and print the pythonpath variable. Here is the script:

#!/bin/bash

source venv/bin/activate
(cd cool/subcool && export PYTHONPATH=`pwd`:$PYTHONPATH)
echo $PYTHONPATH

When I execute it I just get a blanc line, i.e. the $PYTHONPATH is empty and no virtualenv is activated. I am new to bash scripting. What would be the way to make this work? thanks

Upvotes: 2

Views: 2453

Answers (2)

Socowi
Socowi

Reputation: 27215

The cd seems overkill for adding a path to a variable. Why not use readlink -f?

source venv/bin/activate
export PYTHONPATH="$(readlink -f cool/subcool):$PYTHONPATH"
echo "$PYTHONPATH"

Upvotes: 1

Alfe
Alfe

Reputation: 59426

How about this:

source venv/bin/activate
PYTHONPATH=$(cd cool/subcool && echo $(pwd):$PYTHONPATH)
echo $PYTHONPATH

But you should name this file foo.rc or similar and then source it instead of call it (using source or . which is the same):

source foo.rc

Otherwise it does not influence your calling shell.

Upvotes: 2

Related Questions