Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 03-09-2004, 09:12 PM   #1
Registered: Jun 2003
Distribution: mandrake 9.2
Posts: 57

Rep: Reputation: 15
simple question on shell programming

say you have a shell program asdf. what's the difference of running the following commands?

. asdf


chmod u+x asdf ; asdf

Both of them execute the file. But lately, I started to notice subtle differences, for example the variable $0 is different. Anyone with any ideas what's the difference between executing a file . asdf or just asdf with the right permissions? Thanks
Old 03-09-2004, 10:00 PM   #2
LQ Guru
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
The period in the front is a more explicit way to run the program; it says "run the executable file called asdf in the current directory." Without the period, your shell (bash or whatever) is depending upon the $PATH environment variable to know where to find the executable called asdf. If the current directory is not in the $PATH, it won't work to run it without the leading period. There's also the chance that another executable in your $PATH is found first; say, if there was a system-wide utility called asdf, and it was in /usr/bin, it's likely to be found (and run) first. Sometimes, a default $PATH setup will not even include your /home directory, so if that's where the asdf program is, it might not work at all to just use 'asdf'.

You can run 'which asdf' to see the full path name of the file that will be run if you just type 'asdf' to run it.
Old 03-10-2004, 12:13 AM   #3
Senior Member
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
well, that'd be true for './asdf', right? For '. asdf', it's saying to source the file - run it in the current instance. Whereas, with a normal 'asdf' it spawns a subshell to execute. So the environment is going to be different because the subshell isn't a login shell - well, and is just a 'different shell' in general. If the contents of asdf are 'cd; ls' and you run it, you don't go anywhere - your current shell is still where it was when the subshell exits. Whereas if you source it, you will cd and then ls.

I think.
Old 03-10-2004, 10:39 AM   #4
Registered: Jun 2003
Distribution: mandrake 9.2
Posts: 57

Original Poster
Rep: Reputation: 15
so it is better to run any program in the subshell, or the bash shell?
Old 03-10-2004, 12:28 PM   #5
jim mcnamara
Registered: May 2002
Posts: 964

Rep: Reputation: 36
If you want, for example, environment variables to stick around after they are changed, you run commands like this:

. asdf 

# or 
{ asdf }
This makes the script run in your existing process, not a subprocess.
Creating a subprocess or not creating one is only important when you want changes to things to stick around. If you do not want a cd command to actually change your current working directory after the code is done, then don't use the dot command. run the script in a subprocess.

subprocess is another word for "new shell"

If you put she-bang as the first line of a script you get a new shell
cd /somewhere
ls something
creates a bash shell in a subprocess.
Old 03-10-2004, 02:07 PM   #6
Registered: Jun 2003
Distribution: mandrake 9.2
Posts: 57

Original Poster
Rep: Reputation: 15
I see what u guys mean, thanks.

It's quite interesting, a script named casdf, containing echo $0 , runs differently for the two.

casdf returns: ./casdf

. casdf returns: bash


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
simple shall programming question itz2000 Programming 1 11-05-2005 04:12 PM
very simple bash programming question!? Thinking Programming 7 06-01-2005 11:07 AM
Linux Shell programming question ducka Programming 4 09-01-2003 09:24 AM
Simple C Programming Question.. Bolt Programming 6 06-03-2003 12:05 PM
A simple perl programming question J_Szucs Programming 12 07-14-2002 10:03 PM > Forums > Non-*NIX Forums > Programming

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

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