LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-25-2014, 04:17 AM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,485

Rep: Reputation: 51
Difference between ./foo, . foo and source foo?


Hi: I can run a shell script in either of three ways. Let the name of the script be foo:

1) ./foo or just foo if . is in the PATH.
2) . foo
3) source foo

What is the difference between these invocations?
 
Old 04-25-2014, 07:16 AM   #2
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie stretch
Posts: 1,593

Rep: Reputation: 97
Hya

I am a tcsh user. 2) does not work.

cheers
 
Old 04-25-2014, 07:20 AM   #3
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie stretch
Posts: 1,593

Rep: Reputation: 97
Hya

I am a tcsh user.

A bash script can go with situation 1), but not situation 3).

cheers
 
Old 04-25-2014, 08:19 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,121

Rep: Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270
2 and 3 are the same, it means the program will be executed by the current shell. Tcsh cannot understand 2) some other shells cannot understand 3).
1 means a new shell will be opened (child process) and that will execute foo.

The main difference is that using method 1 you can execute a script but your current shell will remain as it was before, using 2 or 3 you will be able to modify your actual environment (define functions, set variables ...)
 
Old 04-25-2014, 10:55 AM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
The '.' notation in bash is just shorthand for 'source'. To 'source' a file is causing the current shell process to read it in mostly the same manner as if you had typed it interactively. This allows you to use that method when you need to alter the properties of the current shell session, such as changing directories or setting environment variables. When a script is 'run', as your "1)" method describes, it launches a child shell which executes the script, and then terminates. Any alteration of process properties is restricted to that child shell. Since the child shell terminates when the script completes execution, those changes vanish with it. Moreover, the child shell (like any process) is incapable of altering the properties of any other process, be it a parent, child or sibling.
 
1 members found this post helpful.
Old 04-25-2014, 08:55 PM   #6
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,485

Original Poster
Rep: Reputation: 51
Aha... When I have just installed an O.S. I must afterwards do some settings and install some software. For instance, one thing I do, is to install two programs: autojump and ytree. To save work, I could write a script like this:
Code:
#!/bin/sh
## Linked to /bin/bash in my system.

### Pseudocode - I don't know how this is done
if ~/work does not exist
  mkdir ~/work
### Pseudocode END

cp /deb4/almacen/soft/ncd/autojump-v8.tar.gz ~/work
cp /deb4/almacen/soft/ncd/ytree-1.94.tar.gz ~/work
cd ~/work
gzip -dv autojump-v8.tar.gz
gzip -dv ytree-1.94.tar.gz
tar -xvf autojump-v8.tar
tar -xvf ytree-1.94.tar

cd joelthelion-autojump-ea68448
./install.sh

# Here I just edit ~/.bashrc and /root/.bashrc 
# when working interactively. So here I do:
echo "source /etc/profile.d/autojump.bash" >> ~/.bashrc
echo "source /etc/profile.d/autojump.bash" >> /root/.bashrc

cd ~/work/ytree-1.94
make
make install
According to what has been said, it would be exactly the same if I source this script or I directly run it (methods 3) and 1) above). Am I right?

Last edited by stf92; 04-25-2014 at 09:03 PM.
 
Old 04-25-2014, 11:23 PM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Yes you could use methods 1, 2, or 3 with that.

I rarely put build commands in scripts though, and I recommend against it as well. Unless you are positive your system is set up properly (all necessary dependencies), it could easily fail. The problem with scripts is, if a command fails, the script just keeps on going. For example, if your "make" fails because of a missing dependency, the script doesn't give a crap and goes straight to "make install". You should really be putting in some error checking if you want this to be a reliable tool.

Last edited by suicidaleggroll; 04-25-2014 at 11:26 PM.
 
Old 04-26-2014, 02:04 AM   #8
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,121

Rep: Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270
yes, you are right. In that case method 1 is preferred. (actually if you are not sure about that you ought to choose the first one).

see mkdir -p about your pseudo code
tar can handle zip files, you do not need gzip but tar xvzf
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] C: isn't foo[30] a pointer? Assume declaration char foo[30]. stf92 Programming 11 01-25-2014 11:31 AM
[SOLVED] Apache: RewriteRule - redirect /foo only works if /foo exists ezekieldas Linux - Software 2 07-31-2012 12:45 PM
Killing foo, which I started as foo &. stf92 Linux - Newbie 9 11-08-2010 11:54 PM
Difference between packages linux-headers-foo and linux-headers-foo-generic? jhwilliams Ubuntu 1 10-19-2009 06:04 AM
Which config file should I use... foo or foo.new? davidguygc Slackware 6 08-01-2007 06:21 PM


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

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration