Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 02-04-2015, 11:21 AM   #1
Registered: Jul 2005
Location: Montreal Canada
Distribution: Fedora 31and Tumbleweed) Gnome versions
Posts: 311
Blog Entries: 1

Rep: Reputation: 59
Post How to:? Need affinity of a sourced shell script to the calling script

Here is a scenario.

/usr/local/bin/ contains two shell scripts, and

my home directory ~/bin contains testing versions of and and point
to different locations

In both cases I want to source the script that is in the same directory containing


So, regular use
I want always use the two scripts from /usr/local/bin
testing use
I want always that the two scripts execute from ~/bin

It does not always work. Different results for different working directories (pwd)
I tested with some bash commands vis...
which, dirname, realpath calling bash -x from either home ~/bin or /usr/local/bin

In some cases the wrong gets invoked. I want to avoid that problem.

If you can help, please test with bash -x from ~ , ~/bin /usr/local/bin

Hints are important.

Herein is my test script

SOXBIN=`dirname $0`		
if [ $SOXBIN == '.' ]; then
echo "executing from `pwd`"
echo "$SOXBIN/ $1 $2"
. $SOXBIN/ $1 $2
I tried the above with just
cd /usr/local/bin ; ./test1

cd ~ ; bin/test1
cd ~/bin ; ./test1

I do software work in Baan ERP systems

Last edited by Lsatenstein; 02-04-2015 at 12:24 PM.
Old 02-05-2015, 03:53 AM   #2
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,789

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
I do not really understand your problem, but probably readlink -m can help you
Old 02-05-2015, 07:05 AM   #3
Registered: Aug 2002
Posts: 25,674

Rep: Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892
$0 returns the name of the program as it was called.

I assume that /usr/local/bin is in your path environment but have you also added ~/bin?

Unlike windows the current working directory path i.e . is not by default in the users path environment.

If you execute test1 from any directory it should return /usr/local/bin since that is in the path environment. The same will be returned if you use the entire path i.e. /usr/local/bin/test1 to execute your script.

./test1 will always return . no matter what since that is how the program was called.

~ is a short cut for /home/username. Therefore ~/bin/test1 should always return /home/username/bin

If your ~/bin is your path environment then it depends on how the path is searched as to which test1 actually executes which is why adding the current working directory is discouraged.


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
calling a shell script present on another server using perl script. anandg111 Linux - Server 4 07-18-2012 12:23 AM
[SOLVED] Help calling a shell script inside a python script WyoChuck Linux - Newbie 4 05-28-2012 09:39 PM
Calling a bash shell script from within another script dedman Linux - Software 7 04-24-2010 08:53 PM
how can a shell script determine its own location when being sourced? DeuceNegative Linux - Software 5 02-21-2008 01:57 PM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-11-2005 11:08 PM > Forums > Linux Forums > Linux - Software

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