LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Shell scripting, difference of "source script.sh" and "./script.sh" (http://www.linuxquestions.org/questions/programming-9/shell-scripting-difference-of-source-script-sh-and-script-sh-823282/)

Squall90 07-31-2010 04:15 PM

Shell scripting, difference of "source script.sh" and "./script.sh"
 
Hey there,

I added created a script /opt/path.sh to add paths for some programs in /opt. The content of the script is:
Code:

#!/bin/sh
export PATH=$PATH:/opt/program/bin

I made it executable and added it to /etc/profile.

But it doesn't execute the script unless I execute it with "source /opt/path.sh"

I have found something (http://programming.itags.org/unix-li...ramming/81171/). According to this, the change is only at run-time. But I thought executing "export" make it global?

Can somebody explain it please?


Thanks

acid_kewpie 07-31-2010 04:24 PM

you should not put a script directly in /opt, that's a really bad place to put anything other than other directories.

source does not run the file, it just inserts it into the current script, and evaluates it as part of the current script. This is typically used for adding in standard functions. It doesn't look like your script deserves it's own file anyway, just add it to your path directly.

druuna 07-31-2010 04:24 PM

Hi,

This (quote from the link you posted):
Quote:

When you use the "." command, the script is executed by the current
shell. If any variables are assigned, these changes will be visible in
the shell after the script completes

When you execute a script the normal way, a new shell process is
started. Any variable changes will take place in that process, but will
have no effect on the original process.
- If you source a file the export makes the variable present in the current shell and all new shells.
- If you execute a script a new shell is created first. The subsequent export var makes the var available in that new shell and its sub-shells.

Hope this clears things up a bit.

carltm 07-31-2010 04:36 PM

It's all about shells. Initially there is only one process running,
and it spawns new processes, including a set of processes that let
you log in.

After you log in, you are running a process with your login shell,
probably bash. When you type most commands, the current shell pauses
while a subshell is created to run the command. After the command
finishes the subshell quits and you are returned to your original shell.

Within your shell you have variables set, and some of them are
exported. The ones that are exported are created in all subshells
automatically. The ones that are not exported do not get created.

So exporting doesn't really make a variable global. It just means
that variable is created in any subshells.

Incidentally, there is one notable command that doesn't create a
subshell, and that is source. When you source a command, the
command is run in the current shell.

If you want your script to work, add "source /opt/path.sh" to
/etc/profile or move the script to /etc/profile.d.

Squall90 07-31-2010 04:40 PM

Quote:

Originally Posted by acid_kewpie (Post 4051202)
you should not put a script directly in /opt, that's a really bad place to put anything other than other directories

Yes, you're right. I'll move it to /etc.

Quote:

Originally Posted by druuna (Post 4051203)
- If you source a file the export makes the variable present in the current shell and all new shells.
- If you execute a script a new shell is created first. The subsequent export var makes the var available in that new shell and its sub-shells.

Hope this clears things up a bit.

Yes it did. So I'll have to source the file if I want it to work or I just add the path in /etc/profile.

Thanks


All times are GMT -5. The time now is 10:50 PM.