LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-23-2009, 08:48 AM   #1
Gecopa
LQ Newbie
 
Registered: Jun 2009
Posts: 6

Rep: Reputation: 0
PATH for Bash


I was reading in the book Beginning Linux Programming about the directories that are listed with PATH that :. means bash will look in the current directory. When I echo PATH on my system :. is not part of any paths in it, however I have no problem running my various little "hello world" programs in the current directories that I work in. I am wondering if this is perhaps because the book I'm reading may be a little out of date and the current versions of bash simply automatically look in current directories. If that is so, is it possible to to put something in the PATH variable that omits looking in the current directory(not that I want to but it would be interesting to know). Thanks for anyone willing to comment on this.
 
Old 06-23-2009, 08:59 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
As far as I know, the current directory must be in your PATH if you want to run commands without specifying the relative or absolute path of the command itself. If I want to run a command in my current directory I have to type
Code:
./command_name
Anyway, there is a chance you have a colon : at the beginning or at the end of your PATH. In that case it includes the current working directory in your PATH. Check it and eventually find out where the erroneous colon has been added.
 
Old 06-23-2009, 09:25 AM   #3
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Bash doesn't include . in the path by default, at least vanilla bash doesn't (of course I can't speak for any odd patches that your distro might include).

colucix said the rest. If you use a standard -unpatched- bash, then you either need to have . in your path or use ./ prefixed to the name of the program that lives in the current directory.
 
Old 06-23-2009, 10:00 AM   #4
Gecopa
LQ Newbie
 
Registered: Jun 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks guys for answering, it cleared up a misconception I was having. For some reason I picked up along the way that the dot " . " was a bash command telling it to execute the file name that follows. So all along I was using ./ to execute my programs in the current directory but misinterpreting what it means.

To test that idea I tried running my program without the ./ prefix and nothing happened, then I added :. to my PATH and when I typed my program name without the ./ prefix name it did run.

So I guess bash will try to run anything you type as long as its been told where to look.

Which brings the question to mind of scripts vs. compiled programs. I haven't done any script writing so this may not be a fair question since I have no concrete examples but from what I understand you can run compiled programs or scripted programs; and if all I need to do is just provide the name of the program and where to look for it, how does bash know to differentiate between a script and a compiled program?

Like with ms windows you would have .exe and whatever for something else. Perhaps I should just research scripting in Linux and come back later to ask if I don't find out.

But thanks again for answering my first question.
 
Old 06-23-2009, 10:17 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by Gecopa View Post
it cleared up a misconception I was having. For some reason I picked up along the way that the dot " . " was a bash command telling it to execute the file name that follows.
This is another story. The dot by itself is a bash built-in equivalent to source. You can do
Code:
. script
to source the script, whereas
Code:
./script
executes the script. The difference is that when you source a script, all the instructions inside it are executed in the current shell and if some environment variable is set, it is preserved as far as you exit from the shell session. In other word, to source a script is equivalent to execute its statements directly from the command line.

On the contrary, when you execute a script you start a child process and all the variables are local to that process and are not inherited by the current parent shell. Eventually they can be inherited from the children of the child (using export) but can't be passed backward to the parents.

Quote:
Originally Posted by Gecopa View Post
how does bash know to differentiate between a script and a compiled program?
Usually the type of a file is retrieved by the content of the first bytes (the header of the file). You can find more information regarding this topic if you look at the manual page of the file command:
Code:
man file
 
Old 06-23-2009, 10:26 AM   #6
Gecopa
LQ Newbie
 
Registered: Jun 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Thank you very much Colucix for that answer. Definitely very useful and informative; will have to play around with some stuff using those thoughts so it sticks to memory.
 
Old 06-23-2009, 10:32 AM   #7
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by Gecopa View Post
So I guess bash will try to run anything you type as long as its been told where to look.
Exactly. "./" is what we call a relative path. It means "from the current directory". You could also specify the whole path, or do "$PWD/my_script.sh". since $PWD will always contain the full path to the current directory.

Quote:
Which brings the question to mind of scripts vs. compiled programs. I haven't done any script writing so this may not be a fair question since I have no concrete examples but from what I understand you can run compiled programs or scripted programs; and if all I need to do is just provide the name of the program and where to look for it, how does bash know to differentiate between a script and a compiled program?
The magic bytes at the beginning of every file tells the shell what to do. Technically the shell is the first to reach at file you want to run. It can identify the type of file based on the contents. If the file is a text file then it tries to run it as a script. If the file starts with the sequence "#!" then the next string is used as the interpreter that must be used to open the file,

Code:
#!/bin/bash
#!/usr/bin/python
#!/usr/bin/perl
#!/usr/bin/awk
Or whatever... This code doesn't interfere with the script because -out of casuality -, the lines starts with #, which is considered a commentary marker

Quote:
Like with ms windows you would have .exe and whatever for something else. Perhaps I should just research scripting in Linux and come back later to ask if I don't find out.

But thanks again for answering my first question.
Win relies on file extensions for this, linux doesn't need to. You can start researching with the "file" command for example. If will use the magic info inside the file to recognize the file type, and it should ignore the extension as long as the file is of a known type and is not corrupt.
 
Old 06-23-2009, 11:21 AM   #8
Gecopa
LQ Newbie
 
Registered: Jun 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Thank you too i92guboj ,
yes I'll have to read the man on file
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script path issue - how to pass a path as a string to a variable PiNPOiNT Programming 5 04-17-2009 05:48 PM
How to rescan bash PATH reakinator Linux - General 7 04-14-2009 03:35 PM
[bash] relative path grezly Programming 1 01-16-2007 02:30 PM
bash path won't stick Lokathor Debian 3 06-13-2006 12:35 PM
Path issue in Bash gd2shoe Linux - Software 2 08-28-2005 10:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:27 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