LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-31-2010, 04:15 PM   #1
Squall90
Member
 
Registered: Oct 2009
Distribution: Currently several distros :S
Posts: 148

Rep: Reputation: 29
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
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 07-31-2010, 04:24 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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.
 
Old 07-31-2010, 04:24 PM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
2 members found this post helpful.
Old 07-31-2010, 04:36 PM   #4
carltm
Member
 
Registered: Jan 2007
Location: Canton, MI
Distribution: CentOS, SuSE, Red Hat, Debian, etc.
Posts: 703

Rep: Reputation: 99
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.
 
2 members found this post helpful.
Old 07-31-2010, 04:40 PM   #5
Squall90
Member
 
Registered: Oct 2009
Distribution: Currently several distros :S
Posts: 148

Original Poster
Rep: Reputation: 29
Quote:
Originally Posted by acid_kewpie View Post
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 View Post
- 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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
Shell script: I have string "abc____def____ghi", how to make "abc def ghi" vouser Programming 8 03-09-2010 10:01 PM
converting shell script from "dialog" to "xdialog" kushalkoolwal Programming 1 02-25-2008 08:40 PM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 07:42 AM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 02:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration