The Computer Oracle

How to use source command in shell script?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Realization

--

Chapters
00:00 How To Use Source Command In Shell Script?
00:34 Accepted Answer Score 8
01:31 Answer 2 Score 11
01:59 Answer 3 Score 3
02:27 Thank you

--

Full question
https://superuser.com/questions/1218586/...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#shell #sh

#avk47



ANSWER 1

Score 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.




ACCEPTED ANSWER

Score 8


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...




ANSWER 3

Score 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.