Ever tried source and sh on some script and it worked with source but not with sh? I had this trouble sometime back. I was working on a python project and was supposed to activate virtual environment before starting off. However, not being very habitual of using virtual environments (I know its a bad practice, I take care now.), I did not remember how to activate it. Though, I knew there was an activate script that needed to be run, so I located it and did
Didn’t work. Hmm. Permission issues.
chmod +x activate
Still won’t work. Then, I looked up the contents of file, it said,
# This file must be used with "source bin/activate" *from bash* # you cannot run it directly
This made me look for differences between some very basic utilities I use everyday.
The file can be a binary executable, or it can start with a shebang line.
When you run
./executable, what shell will be run essentially depends on the shebang written at the beginning of the executable script.
sh initiates a sub-process that runs a new session of
/bin/sh, which is usually a symbolic link to
. filename [arguments] source filename [arguments] Read and execute commands from filename in the current shell environment and return the exit status of the last command exe‐ cuted from filename. If filename does not contain a slash, file names in PATH are used to find the directory containing file‐ name. The file searched for in PATH need not be executable. When bash is not in posix mode, the current directory is searched if no file is found in PATH. If the sourcepath option to the sh opt builtin command is turned off, the PATH is not searched. If any arguments are supplied, they become the posi‐ tional parameters when filename is executed. Otherwise the positional parameters are unchanged. The return status is the status of the last command exited within the script (0 if no commands are executed), and false if filename is not found or cannot be read.
When a script is run using source it runs within the existing shell, any variables created or modified by the script will remain available after the script completes.
$ type source
source is a shell built-in
There’s no difference between source and . (this is not a full stop)
So, that’s what happened. The script activate remained inside the same shell only when invoked with source (or .) which is exactly what was needed.
Thanks for reading.