0

This three line bash script that I wrote,

#! /bin/bash
cd /home/me/path/to/project
source ../../bin/activate

is located in the directory '~/scripts'. I wrote it in order to make it faster to move to a subdirectory for a project that has a long name. This project is written in Python so the second line

source ../../bin/activate

activates the virtualenv where the depencies are installed. However, when I run,

bash script.sh

in '~/scripts' nothing happens. I have run the code manually and verified that it works correctly. Additionally, today is the first time I have observed this behavior. Am I missing something obvious? Why doesn't this work? The only related action I have taken between the last time I ran the script and now was to copy the directory housing the entire project onto a backup hard drive. I am on Ubuntu 18.04.5 LTS

  • The most crucial fragment (from one of the answers to the linked duplicate): "Use source if you want the script to change the environment in your currently running shell. use execute otherwise." If this doesn't solve the problem then leave me a comment, we will reopen the question. In this case please [edit] the question and state what should happen (what is source ../../bin/activate supposed to do?). Any error messages? – Kamil Maciorowski Aug 24 '21 at 04:12
  • That isn't the problem, commenting out the line with 'source' does not change what happens when I run 'bash script.sh'. Instead of changing the directory that I am in my working directory remains '~/scripts'. I have manually ran the script line by line and can verify it works correctly when I do this. It just does not work when I run 'bash script.sh', and I do not understand why. – Ashamandarei Aug 24 '21 at 04:21
  • "Instead of changing the directory ..." -- You need to run . script.sh (equivalent to source script.sh) instead of bash script.sh. This is sourcing vs executing the linked duplicate talks about. To have the question reopened you need to clearly state sourcing the script in question does not work. – Kamil Maciorowski Aug 24 '21 at 04:22
  • Ah, I see what you mean. Sorry, I understand what is going on now, thank you! – Ashamandarei Aug 24 '21 at 04:26
  • A hint: the shebang (#! /bin/bash in your case) is irrelevant when you source the script (. script.sh) or execute an interpreter explicitly with the script as an argument (bash script.sh). It's only relevant when you run the script directly (./script.sh). Running the script directly is not what you want here. I'm only pointing out the shebang is irrelevant in your use case. When you source a script, the shebang in it is only a comment. – Kamil Maciorowski Aug 24 '21 at 04:33

0 Answers0