在Bash脚本中激活virtualenv无效

在Bash脚本中激活virtualenv无效,bash,virtualenv,Bash,Virtualenv,编写脚本以自动化我的flask环境设置 if [[ -z $1 ]]; then echo "usage: flaskup <dirname> <template dir>"; exit else virtualenv $1 && cd ./$1 && source bin/activate && bin/pip

编写脚本以自动化我的flask环境设置

if [[ -z $1 ]];
    then
        echo "usage: flaskup <dirname> <template dir>";
        exit
    else
        virtualenv $1 &&
        cd ./$1 &&
        source bin/activate &&
        bin/pip install flask &&
        mkdir ./app &&
        mkdir ./app/static &&
        mkdir ./app/templates && 
        exit;
fi
if[-z$1]];
然后
echo“用法:flaskup”;
出口
其他的
virtualenv$1&&
cd./$1&&
源箱/激活&&
箱子/管道安装烧瓶&&
mkdir./app&&
mkdir./app/static&&
mkdir./app/templates&&
出口
fi

我希望它将我留在它创建的目录中,虚拟环境处于激活状态,但是它将我留在运行脚本的同一目录中。如何使脚本在激活的虚拟环境中与shell一起退出?

如果在自己的shell中运行脚本(以
/path/to/script
script
运行,如果脚本位于
$path
中),则无法获得所需的内容。运行脚本的shell与从中运行脚本的shell不同,因此无法更改父shell的状态。最接近的方法是让脚本将路径作为输出进行回显,并以
cd“$(/path/to/script)”或类似方式运行它

或者,如果以
的方式运行脚本/path/to/script
(或类似)然后使用当前shell运行它,它所做的任何目录更改都将发生在当前shell中,而不是子shell中