无法在虚拟环境或 macOS 上运行 sh

huangapple go评论57阅读模式
英文:

Can't run sh on virtual env or macOS

问题

我无法在虚拟环境或 macOS 上运行 sh 脚本。

脚本如下:
#!/bin/bash

# This script sets up the environment for a Flask project.

echo "Starting script..."

# Initialize virtual environment.
echo "Activating virtual environment..."
source env/bin/activate

# Define environment variables.
echo "Setting environment variables..."
export FLASK_APP=app.py
export FLASK_ENV=development

echo $FLASK_APP
echo "Script completed."

结果只显示了 echo 路径,但 source 和 export 命令都不起作用。

(base) user@xxx % sh envset.sh    
Starting script...
Activating virtual environment...
Setting environment variables...
application.py
Script completed.
(base) user@xxx %

虚拟环境未被激活。
英文:

I can't run the sh script on virtual env or macOS

#!/bin/bash

# This script sets up the environment for a Flask project.

echo "Starting script..."

# Initialize virtual environment.
echo "Activating virtual environment..."
source env/bin/activate


# Define environment variables.
echo "Setting environment variables..."
export FLASK_APP=app.py
export FLASK_ENV=development

echo $FLASK_APP
echo "Script completed."

The result only shows the echo path but neither the source or the export commands work.

(base) user@xxx % sh envset.sh    
Starting script...
Activating virtual environment...
Setting environment variables...
application.py
Script completed.
(base) usser@xxx % 

The env wasn't activated.

答案1

得分: 1

(base) user@xxx % sh envset.sh    
你在这里调用一个shell,作为终端shell的子进程。子进程永远不会更改其父进程的环境。相反,你应该使用`.`(可移植的`source`)执行文件,它将在终端当前的shell进程中执行其命令。然后,在`source`完成后,exports将对终端shell可用:

(base) user@xxx % . envset.sh    
英文:

> ```
> (base) user@xxx % sh envset.sh

you're invoking a shell here as a child process of the terminal's shell. a child process never changes its parent process's environment.

Instead you should . (portable source) the file which will execute its commands in the terminal's current shell process. Then, the exports will be available to the terminal shell after the source completes:

(base) user@xxx % . envset.sh    

huangapple
  • 本文由 发表于 2023年2月18日 09:52:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490702.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定