6

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

#!/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:

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

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

3
  • Make sure you're running it with bash via #!/bin/bash in the top and sh start.sh. Also source has an alias . so try . ~/env/lib/bin/activate Commented Sep 21, 2015 at 2:34
  • I have #!/bin/bash in my script, and . ~/env/lib/bin/activate is ok, but why source is not ok? Commented Sep 21, 2015 at 2:37
  • Rather, don't use sh start.sh. Run the script with ./start.sh. Don't explicitly invoke the shell. Commented Sep 21, 2015 at 2:43

1 Answer 1

15

Note that the shebang line:

#!/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.


You need to either:

chmod +x script.sh
./script.sh

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

bash script.sh
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.