[sf-lug] 'source' works but 'shebang' doesn't

Akkana Peck akkana at shallowsky.com
Fri Aug 26 11:44:36 PDT 2016


Alex Kleider writes:
> Mystery:
> 
> alex at X301n3:~/Py/Backup/Backup$ cat ./path.sh
> #!/bin/bash
> # Add current working directory to the PYTHONPATH.
> export PYTHONPATH="${PYTHONPATH:+$PYTHONPATH:}$(pwd)"
[ ... ]
> I don't understand how
> source ./path.sh
> does the job but
> ./path.sh
> does not!
> 
> Can anyone explain?

It is working -- just not the way you expect.

When you run ./path.sh, it looks at the shebang and runs the shell
you specify there -- as a subshell of the one you're typing in. That
subshell reads the lines of the script and exports PYTHONPATH in
that subshell, and when it's done reading the script, it exits,
leaving you in your original shell, which still has its original
environment.

When you source ./path.sh, "source" explicitly tells your current
shell to run the commands in the script *in the current shell*.
So this time, export modifies your current shell rather than
executing a subshell.

Source also ignores the shebang line, since you've explicitly told
it you want your current shell to execute it, not some other
program. Even if you have a shebang line that says something like
#!/usr/bin/env python, with source your shell will try to execute it
as a shell script. Try it! It's especially amusing if your python
program starts with import lines: I'll leave the question of what
it's doing in that case as a fun exercise for the reader.

        ...Akkana




More information about the sf-lug mailing list