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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32686520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:36:02  来源:igfitidea点击:

source: not found in shell script?

bashshellvirtualenv

提问by roger

I want to activate a virtual env in shell script, so I write a simple script as follow:

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

#!/bin/bash

source ~/env/lib/bin/activate
#nohup python mock_run.py
#echo $! > save_pid.txt

I start the script with sh start.sh, but I got error as follow:

我用 启动脚本sh start.sh,但出现如下错误:

start.sh: 3: start.sh: source: not found

I run source ~/env/lib/bin/activateis ok, so why can not in shell script?

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

回答by hek2mgl

Note that the shebangline:

请注意shebang行:

#!/bin/bash

is not in effect when you invoke the script with

当您调用脚本时无效

sh script.sh

The shebang is in effect only if you call the script directly, like a binary.

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



You need to either:

您需要:

chmod +x script.sh
./script.sh

to make the shebangline working, or call it explicitly with bash:

使shebang行工作,或使用 bash 显式调用它:

bash script.sh