LinuxQuestions.org
Help answer threads with 0 replies.
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 05-25-2012, 06:38 AM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
bash script export path


I see this as the first line after the shebang in the bashscripts at work

I really don't understatnd what is does -
Code:
export PATH="`dirname "$0"`:$PATH"; . _env.sh
My scripts seem to work just fine without it .
 
Old 05-25-2012, 06:40 AM   #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
it will add the directory that the script is in to its path, whatever it is. Yuck. better off without it.
 
Old 05-25-2012, 07:17 AM   #3
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
I kinda thought that it opens up an new shell for iteration of the loop.

What use is there in exporting the path
 
Old 05-25-2012, 07:19 AM   #4
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
loop? what loop?
 
Old 05-25-2012, 09:49 AM   #5
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
Yeah - forget the loop.
I though maybe that this would open up the program in an independend shell, and each loop gets send to antoher shell.
that is not it though.


What does the . mean in bash.
In perl it means a concatination - and I though that is what is was doing here - concatatining this directory to executable path.
but it is not.
And when I look up period bash in google I get nothing

What is the period in bash mean (it is not a regular epressions either)
 
Old 05-25-2012, 10:46 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
In this case "." is the source command. There is no need to do anything special for concatenation in the shell. Just stick things together. The PATH setting command you asked about is doing just that.

See here for a fairly-complete list of all specially-treated characters in bash:

http://www.tldp.org/LDP/abs/html/special-chars.html
 
1 members found this post helpful.
Old 05-25-2012, 10:55 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Looks like there is/was an optional _env.sh script in each directory with shellscript files. If _env.sh exists it would work like a shell "library", from the name probably used to configure the shell/scripting environment and/or set environment variables.

BTW the ";" at the end of export PATH="`dirname "$0"`:$PATH"; terminates the first command so the whole line is functionally equivalent to (and more obscure than):
Code:
export PATH="`dirname "$0"`:$PATH"
. _env.sh

Last edited by catkin; 05-25-2012 at 10:57 AM. Reason: clarity
 
Old 05-25-2012, 12:55 PM   #8
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
Hmm, but a source doesn't use $PATH. "_env.sh" will always be in the current directory. I'd be more given to think it was to maybe override system programs? a copy of ls in the current directory woul be used in preference to /bin/ls
 
Old 05-25-2012, 11:18 PM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by acid_kewpie View Post
Hmm, but a source doesn't use $PATH. "_env.sh" will always be in the current directory.
Nope, source does use path:
Code:
~$ help .
.: . filename [arguments]
    Execute commands from a file in the current shell.
    
    Read and execute commands from FILENAME in the current shell.  The
    entries in $PATH are used to find the directory containing FILENAME.
    If any ARGUMENTS are supplied, they become the positional parameters
    when FILENAME is executed.
    
    Exit Status:
    Returns the status of the last command executed in FILENAME; fails if
    FILENAME cannot be read.
 
Old 05-26-2012, 12:10 AM   #10
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
For what its worth, I've used
Code:
#!/bin/sh
BASE="`dirname "$0"`"

[ -r "$BASE/filename.sh" ] && . "$BASE/filename.sh"
to source another script from the same directory as the original script when writing widely compatible Bourne shell/POSIX shell scripts that cannot rely on being installed in a specific directory. Note that if you do not need to be compatible with ancient versions of /bin/sh, it is better to use BASE="$(dirname "$0")"

Note that sourcing another file means the other file is essentially "included", executed using the same shell instance. In particular, all variables and functions set by the other file will be available to the original script too. This is often used for setting configuration values, so that they only need to be updated in one file only. For an example, look at your service scripts in /etc/init.d/. (Note however that they source their files using fixed paths.)

Although you often see the test flag using -e (exists) or -x (is executable), the -r (is readable) is more appropriate. You do not need execute rights to source a file, only read rights.
 
  


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] Export path and edit bash vodka_linux Linux - Newbie 3 01-07-2011 02:41 PM
bash script path issue - how to pass a path as a string to a variable PiNPOiNT Programming 5 04-17-2009 05:48 PM
export call in bash script Donald1000 Linux - Software 10 03-12-2009 08:03 PM
export $PATH inside script gmitra Programming 3 02-01-2005 08:34 AM
set and export using bash script acummings Linux - General 10 01-03-2005 02:22 PM

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

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