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

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

Can't run sh on virtual env or macOS

问题

  1. 我无法在虚拟环境或 macOS 上运行 sh 脚本。
  2. 脚本如下:
  3. #!/bin/bash
  4. # This script sets up the environment for a Flask project.
  5. echo "Starting script..."
  6. # Initialize virtual environment.
  7. echo "Activating virtual environment..."
  8. source env/bin/activate
  9. # Define environment variables.
  10. echo "Setting environment variables..."
  11. export FLASK_APP=app.py
  12. export FLASK_ENV=development
  13. echo $FLASK_APP
  14. echo "Script completed."
  15. 结果只显示了 echo 路径,但 source export 命令都不起作用。
  16. (base) user@xxx % sh envset.sh
  17. Starting script...
  18. Activating virtual environment...
  19. Setting environment variables...
  20. application.py
  21. Script completed.
  22. (base) user@xxx %
  23. 虚拟环境未被激活。
英文:

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

  1. #!/bin/bash
  2. # This script sets up the environment for a Flask project.
  3. echo "Starting script..."
  4. # Initialize virtual environment.
  5. echo "Activating virtual environment..."
  6. source env/bin/activate
  7. # Define environment variables.
  8. echo "Setting environment variables..."
  9. export FLASK_APP=app.py
  10. export FLASK_ENV=development
  11. echo $FLASK_APP
  12. echo "Script completed."

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

  1. (base) user@xxx % sh envset.sh
  2. Starting script...
  3. Activating virtual environment...
  4. Setting environment variables...
  5. application.py
  6. Script completed.
  7. (base) usser@xxx %

The env wasn't activated.

答案1

得分: 1

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

  1. (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:

确定