14

I would like to use source command in shell script.

What I have done is like this below:

in start.sh

source ~/tensorflow/bin/activate

then in command line run this script.

$sh start.sh

However nothing happens and

$which source

command doesn't show anything.

So, source is not the normal command?

Is this wrong idea? or how can I reduce the type of path?

1
  • 1
    Quick answer: source is BASH SHELL BUILTIN command. See man bash (search for "source filename [arguments]" to find it). It doesn't work in question probably because you are using the SH shell (something besides BASH anyway) as your login shell, or inside your script. Use BASH if you want the source command.
    – B. Shea
    Apr 21, 2023 at 1:48

3 Answers 3

7

source is a shell built-in command. The which command looks for binaries on the PATH, like in /usr/bin, /bin, /sbin, etc. but you won't find any built-in commands in a separate binary.

Also, having the source command in a shell script does not result in the source propagating up to your current shell when you run it. sh blah.sh where blah.sh has source in it will not actually source the contents of the file into your interactive shell. That's not how sourcing works.

If you want this sourcing of the tensorflow activate script to happen every time you open a new shell, you need to edit ~/.bashrc or ~/.profile(or other files, depending on what your shell is and how it's configured) and put thesource` command directly in there.

P.S. - your question title is very confusing and looks incomplete. Take some time to edit, revise and clean up your post, or you run the risk of someone downvoting it :P I'm tempted to do so myself, but I wrote an answer, so I'm a little bias...

4
  • I understood. source command is not the binary command. And sorry for bothering you with incomplete title I sent accidentally before editing finished. I updated the title and article. thank you very much for your kindness.
    – whitebear
    Jun 14, 2017 at 1:37
  • 1
    "source is a shell built-in command." should read "source is a BASH shell built-in command." Please clarify your 1st paragraph/answer. It's too short an edit for me.
    – B. Shea
    Apr 21, 2023 at 1:51
  • 1
    I believe the OP was calling the shell script using sh instead of bash. I had a similar issue and @B.Shea comments helped, so I simply changed my shebag to read #! /usr/bin/bash and now my shell script works as expected. However, I see what you're getting at in this answer b/c the sourced commands only exist in the shell and disappear once it exits. Jul 16, 2023 at 9:06
  • @MarkMikofski Yup :-) - Also, I have corrected answer: 'is a shell built-in' to 'is a Bash shell' and 'sh blah.sh' to 'bash blah.sh' - the shell matters in this case as we have mentioned.. in addition to rest of answer.
    – B. Shea
    Jul 16, 2023 at 13:55
11

Please use the command:

source start.sh

I came across this very problem, when I wanted to avoid writing:

source ~/.bashrc
source activate tensorflow
jupyter notebook

Thereafter, I came across a page which explains this better: What are the differences between executing shell scripts using “source file.sh”, “./file.sh”, “sh file.sh”, “. ./file.sh”? on Ask Ubuntu.

1
  • 1
    OMG! so obvious in hindsight, yet I would have never thought to run the script with source, thanks for the insight. May 28, 2019 at 19:17
3

It appears that you wish to activate a python virtual environment where you installed tensorflow. You can try using an alias in ~/.bashrc.

Add

alias pytensorflow='source ~/tensorflow/bin/activate'

at the end of ~/.bashrc, below the line

# User specific aliases and functions

Start a bash session and you can simply type in pytensorflow at the command prompt to activate the virtual environment. I have successfully used this.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .