LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 03:17 AM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
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, 06:16 AM   #2
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: SlackWare > Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy .. bullseye bookworm
Posts: 1,832

Rep: Reputation: 108Reputation: 108
Hya

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

cheers
 
Old 04-25-2014, 06:20 AM   #3
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: SlackWare > Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy .. bullseye bookworm
Posts: 1,832

Rep: Reputation: 108Reputation: 108
Hya

I am a tcsh user.

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

cheers
 
Old 04-25-2014, 07:19 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
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, 09:55 AM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
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, 07:55 PM   #6
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
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 08:03 PM.
 
Old 04-25-2014, 10:23 PM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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 10:26 PM.
 
Old 04-26-2014, 01:04 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:09 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
Open Source Consulting | Domain Registration