来源:在shell脚本中找不到?

rog*_*ger 2 bash shell virtualenv

我想在shell脚本中激活虚拟环境,所以我写了一个简单的脚本如下:

#!/bin/bash

source ~/env/lib/bin/activate
#nohup python mock_run.py
#echo $! > save_pid.txt
Run Code Online (Sandbox Code Playgroud)

我用脚本启动脚本sh start.sh,但是我得到如下错误:

start.sh: 3: start.sh: source: not found
Run Code Online (Sandbox Code Playgroud)

我运行source ~/env/lib/bin/activate没问题,为什么不能在shell脚本中?

hek*_*mgl 6

请注意shebang线:

#!/bin/bash
Run Code Online (Sandbox Code Playgroud)

使用时调用脚本时无效

sh script.sh
Run Code Online (Sandbox Code Playgroud)

仅当您直接调用脚本时,shebang才有效,就像二进制文件一样.


你需要:

chmod +x script.sh
./script.sh
Run Code Online (Sandbox Code Playgroud)

使shebang行工作,或用bash明确调用它:

bash script.sh
Run Code Online (Sandbox Code Playgroud)