Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using 'source' in shell scripts

Status
Not open for further replies.

analogduck

Programmer
Jan 10, 2002
12
US
When writing shell scripts (for bash and tsch), why is it that when a script calls another script from within it that the second script is 'source'd? Why can't the second script just be called like any other command which is called (without a 'source' preceeding it)?

Must you always 'source' secondary scripts which are called? Or are there exceptions to the rule? Must you 'source' even perl/python scripts, or just other tcsh/bash scripts?

'source' is mentioned briefly enough in the 'tcsh' and 'bash' man pages, but they don't bother explaining why it needs to be used.

Your knowledge is appreciated. Thank you. ;)

-- Sean
 
'man exec' doesn't do it for me. But I did receive this helpful and well-written reply from a friend by email. (I'm posting here in case it helps anyone else).

"UNIX programs can NEVER, ever modify the environment of their parents. A
program can only modify the environment that later will be passed to its
children.

Example:

You write a program that executes a 'cd' command. That 'cd' will be
effective during the run of that program, but when it completes, you will
be back at the old PWD (present working directory) when that program was
executed.

The 'source' command is a shell built-in command, that is, part of the
shell's internal code. Normally when a command is executed, the shell
FORKS a child process to execute said command, so that any changes made
will NOT effect the original parent shell. The source command causes the
program to be executed in the CURRENT shell, so that any variables already
set within the file will become part of the environment in the current shell.

So to answer you questions, if you call a script versus sourcing it, you
lose the environmental variables (usually a bad thing) and can lose job
control or at least feedback on status of completion {success or failure}.

So, if you don't need environment variables passed from parent to child,
just call instead of source.

Shells with job control use source - csh (its derivatives like tcsh) and
bash (and its derivs).

Shells with no job control usually use '.' instead of source.

If you call perl/python from a shell script, make sure that the ENV gets
read in from somewhere. I defer to Tim/Bryan on perl questions. I am
retarded when it comes to perl.

I know you can send commands like:

csh/tcsh applicable

(setenv PATH "/bin:/home/sean/bin"; perl myperlprog)

sh/bash applicable

PATH='/bin:/home/sean/bin' perl myperlprog


>> Your knowledge is appreciated. Thank you. ;)


No worries at all. I hope this helps.

Mike"

Wow! That answers all my questions very well!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top