bash: Where Am I?

  • Permalink
  • submit to reddit
  • Email
  • Follow


Hi everybody.

I have a bash script I've created that needs to know what directory it's
in. I can't use pwd, because that will give me the directory from where
the script was run, and not where it actually is on the disk. Can anyone
suggest a way of doing this?

Thanks in advance,
Steve.

0
Reply Steve 5/20/2006 1:03:00 PM

See related articles to this posting


Steve Jones wrote:

> I have a bash script I've created that needs to know what 
> directory it's in. I can't use pwd, because that will give 
> me the directory from where the script was run, and not where 
> it actually is on the disk. Can anyone suggest a way of 
> doing this?

How about "dirname $0"?

Cheers, 

-- 
Klaus Alexander Seistrup
SubZeroNet, Copenhagen, Denmark
http://magnetic-ink.dk/41
0
Reply Klaus 5/20/2006 1:32:01 PM

On Sat, 20 May 2006 13:32:01 +0000, Klaus Alexander Seistrup wrote:

> Steve Jones wrote:
> 
>> I have a bash script I've created that needs to know what 
>> directory it's in. I can't use pwd, because that will give 
>> me the directory from where the script was run, and not where 
>> it actually is on the disk. Can anyone suggest a way of 
>> doing this?
> 
> How about "dirname $0"?
> 
> Cheers,

Of course - it's obvious now you mention it. Silly me.

Thanks a lot,
Steve.

0
Reply Steve 5/20/2006 3:25:05 PM

In article 
<pan.2006.05.20.15.25.05.195595@squaregoldfish.NOSPAM.co.uk>,
 Steve Jones <steve@squaregoldfish.NOSPAM.co.uk> wrote:

> On Sat, 20 May 2006 13:32:01 +0000, Klaus Alexander Seistrup wrote:
> 
> > Steve Jones wrote:
> > 
> >> I have a bash script I've created that needs to know what 
> >> directory it's in. I can't use pwd, because that will give 
> >> me the directory from where the script was run, and not where 
> >> it actually is on the disk. Can anyone suggest a way of 
> >> doing this?
> > 
> > How about "dirname $0"?
> > 
> > Cheers,
> 
> Of course - it's obvious now you mention it. Silly me.

Now go read the FAQ and learn the limitations of this.  It's not 
possible to do it fully portably and generally.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
0
Reply Barry 5/20/2006 10:49:14 PM

curdir=$(cd `dirname $0`; pwd)

0
Reply kevjunk 5/21/2006 3:36:27 PM


Steve Jones wrote:
> Hi everybody.
> 
> I have a bash script I've created that needs to know what directory it's
> in. I can't use pwd, because that will give me the directory from where
> the script was run, and not where it actually is on the disk. Can anyone
> suggest a way of doing this?
> 
> Thanks in advance,
> Steve.
> 

Try the ksh type command.  If someone enters just the script name
and the shell uses PATH to find it, dirname $0 will return just a dot.
The type command returns the full path.

scriptpath=`ksh -c "type \"$0\""`

Bill Seivert

0
Reply Bill 5/22/2006 12:52:07 AM
comp.unix.shell 15064 articles. 25 followers. Post

5 Replies
400 Views

Similar Articles

[PageSpeed] 20


  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

BASH: Can bash do this?
I input a long command in bash, and then I found I must run some other command such as man, but I must not run this long command to save it in the history, because it may harmful, Is there a way to save it in the history without running it? On 17 Dec 2006 19:10:48 -0800, "hantechs@gmail.com" <hantechs@gmail.com> wrote: >I input a long command in bash, and then I found I must run some other >command such as man, but I must not run this long command to save it in >the history, because it may harmful, Is there a way to save it in the >history without running it? #c...

Bash /etc/bash.bashrc
Hi I have recently installed bash from IBM toolbox and I'm trying to set up /etc/bash.bashrc but it seems (truss says so) that version for AIX uses only rc files in user home directories. I need to set up dates in bash history with environment variable HISTTIMEFORMAT="%FT%T" because bash doesn't use EXTENDED_HISTORY. How to force bash to read some global configuration file? Thanks for help in advance -- Pozdrawiam Filip Kata In news:fim3hj$7gb$1@inews.gazeta.pl, Filip Kata <imie@imienazwisko.com> typed: > Hi > > I have recently installed bash from ...

(patch for Bash) XML + Bash
After looking at Expat XML parser (www.libexpat.org), I've decided to patch a simple interface to Bash shell. Mainly, so that Gawk can't brag about it having an interface. :-) In C code, you would register callback functions, and Expat will call them as it encounters various XML components. So, I made it similar in shell. That is, you specify shell functions/commands to be called. You still have to do some bookkeeping, but I think it's easier in shell. Of course, you can do other things in C which you can't do in shell. Ref: http://freshmeat.net/projects/bashdiff/ -...

Bash calls TCL, TCL calls Bash, 2nd Bash never reads input
I'm sorry but it's the ONLY way I can write this. I don't know how to read standard in in TCL; I don't know how to do complex string manipulation in Bash. So I have to write in the languages based on what I know. The bash script cmds.sh will read in various files. Check it out: # # Check to see if the user has entered a valid option if any # if ! [ $1 ]; then echo echo 'You must enter an option: ' echo '------------------------- ' echo " $0 -t = Transfer files " echo " $0 -a [--transferfiles] [--delete] [-d directory] [-f file(s)|*.*] ...

Bash
Hi all, I've reinstalled all the djgpp onto my new disk. I seem to remember that invoking bash from a dos bat file worked and it still does. What I also remember and that seems to be correct from looking at the docs is that the _bashrc file should be run under bash so as it can configure itself and I can set the prompt to PS1='\w-->' cwd. The _bashrc file does not run. I also notice from my bash book that this should use double quotes " not single '. It does what I want using source _bashrc from the bash command line. Any comments? Regards Ian. T...

bash
Does bash really mean Bunch of Assholes Spouting Hogwash? kevin wrote: > Does bash really mean > > Bunch of Assholes Spouting Hogwash? > > Yes. As much per se, as the man on the moon is frowning upon you at night.. ...

Bash
I have been banging my head against a wall trying to make a 2-way pipe work in bash. It works in Korn shell and shoudl work in bash but I keep gettin the same error I go :- more file|& and get the error message -bash: syntax error near unexpected token `&' In ksh it is fine My bash version is GNU bash, version 2.05b.0(1)-release (i586-suse-linux) running on SCO Linux (I know, I know, don't hate me for it) Any ideas ? Tim Pailthorpe wrote: > I have been banging my head against a wall trying to make a 2-way pipe work > in bash. > > It works in Korn she...

Bash
Hi, I would like the users get the prompt "bash" whne they log in . How can I do this ??? Thanks "bernard" <bernard_reims@hotmail.fr> writes: > I would like the users get the prompt "bash" whne they log in . How can I do > this ??? Install bash (from ports) and make it the users' default shell (with chsh(1), vipw(8), pw(8), etc...). -- Lowell Gilbert, embedded/networking software engineer http://be-well.ilk.org/~lowell/ On 22 Dec 2005 10:23:19 -0500, Lowell Gilbert wrote: >> I would like the users get the ...

BASH
I haven't done anything with a shell script since I got rid of Tandy XENIX and my Tandy Model 16. Can anyone point me to a web site that has bash for dummies or something similar? cmsix On Sat, 13 Sep 2003 21:25:37 GMT, cmsix wrote: > I haven't done anything with a shell script since I got rid of Tandy XENIX > and my Tandy Model 16. Can anyone point me to a web site that has bash for > dummies or something similar? http://www.tldp.org/LDP/abs/html/index.html "Bit Twister" <BitTwister@localhost.localdomain> wrote in message news:slrnbm7bq...

bash
When I press the TAB key bash would present a list of choices through its completion facility; now how can I change the key assignement so instead of TAB I would press ESC for the same thing, or how do I make it react to two TABs, not just one ? ...

(patch for Bash) GTK+2 + Bash
I'm very excited to announce shell interface to GTK+2 (2.6.1) for Bash. It read XML syntax describing the widget layout, and returns user selection as shell variable or runs shell command as callback. It's designed for simple GUI dialog or layout, with the emphasis on getting the user data back into shell. For the moment, the shell variable and command are disabled. It just prints to stdout, instead. But, you can change it easily. Ref: http://freshmeat.net/projects/bashdiff/ http://home.eol.ca/~parkw/index.html#gtk Usage: gtk < file.xml gtk file.xml... For...

(patch for Bash) XML + Bash #2
After looking at Expat XML parser (www.libexpat.org), I've decided to patch a simple interface to Bash shell. Mainly, so that Gawk can't brag about it having an interface. :-) In C code, you would register callback functions, and Expat will call them as it encounters various XML components. So, I made it similar in shell. That is, you specify shell functions/commands to be called. You still have to do some bookkeeping, but I think it's easier in shell. Of course, you can do other things in C which you can't do in shell. Ref: http://freshmeat.net/projects/bashdiff/ ...

(patch for Bash) Bash + PostgreSQL + MySQL
Basic interface to PostgreSQL and MySQL has been added to Bash: http://freshmeat.net/projects/bashdiff/ Intent is not to replace 'mysql' and 'psql' which are the command line client program. But, to make it easy to send data and receive data from database server. -- William Park, Open Geometry Consulting, <opengeometry@yahoo.ca> No, I will not fix your computer! I'll reformat your harddisk, though. ...

bash or what?
Hello everybody, I hope I'm not OT, in which case I apologize. ## YOU CAN SKIP THIS PREAMBLE <skip> I've developed a reasonably structured website, whose generation is 99% automated by using a number of tools and glueing them together with some 2,000 lines of bash code. Tools that I use most are JAlbum, imagemagick, but I do quite a lot of text manipulation because I need to change or generate html code. </skip> ## THIS IS THE ACTUAL POST <post> My shell application runs on a 3GHz 1Gb PentiumIV laptop, and one full run takes about 2 hours. Since the whole system is ...

$[ in bash
I happened to notice the $[ construct in Bash but couldn't find any reference to it in the info page. It seems to do expression evaluation, just like $((. For example, both 'echo $(( $RANDOM % 100 ))' and 'echo '$[ $RANDOM % 100 ]' seems to do that same thing (I'm using bash 4.0 by the way). Could somebody explain/point to relevant doc please? Thanks -- Jeenu On 2010-12-09, Jeenu <groups@jeenuv.otherinbox.com> wrote: > I happened to notice the $[ construct in Bash but couldn't find any > reference to it in the info page. It seems to d...

Bash
Hello, I am using $ bash --version GNU bash, version 2.05b.0(1)-release (i586-mandrake-linux-gnu) Copyright (C) 2002 Free Software Foundation, Inc. When I store the result of a command substitution in a variable and 'echo' it to stdout, the multibyte characters do not appear. These multibyte characters seem to be stripped out by Bash: # wanted result $ wc -c lib.ja.po 590 lib.ja.po # attempt 1 $ text=$( < lib.ja.po ) ; echo ${#text} 462 # attempt 2 $ text=$( < lib.ja.po ) ; echo $text | wc -c 451 # attempt 3 $ ( while read -r text ; do echo $text ; done < lib.ja.po ) | w...

(patch for Bash) Embedded web server + Bash
I added shell interface to Libwebserver-0.6.0, http://stdio.linuxkafe.com/coding/projects/libwebserver-0.6.0-18.zip which is web server library. The official version of Libwebserver is still 0.5.3, and support for that version will be added soon. Yes. You can now run embedded web server directly from shell script, totally independent of any external program. Serving out 2k file is about 7x slower than Apache. We're talking about shell script, so it's fast enough. :-) Ref: http://home.eol.ca/~parkw/index.html#libwebserver Example: func () { echo 'Content-...

shellshock bash bug on SCO? more then just bash
Just testing to see if SCO 5.0.7 has the reported bash bug. > The scripts from https://shellshocker.net/ specify tests for vulnerability to of the shellshock bug: > >> Exploit 1 (CVE-2014-6271) >> >> There are a few different ways to test if your system is vulnerable >> to shellshock. Try running the following command in a shell. >> >> env x='() { :;}; echo vulnerable' bash -c "echo this is a test" >> >> If you see "vulnerable" you need to update bash. Otherwise, you should be good to go. When...

Bash: How to invoke executable for bash with bashrc env setup?
Hi, For example, I set alias in .bashrc alias gnuplot='/remote/atg2/GNUPLOT/bin/gnuplot-4.0.linux' In a c program, I called the following function. assert(system("which gnuplot")!=-1); But the output is still "/usr/bin/gnuplot", which doen't reflect the change in enviroment. I plan to explicitly use bash to invoke gnuplot with the rcfile. Do you know how to do that? Best wishes, Peng On Wed, 25 May 2005 17:20:00 -0700, PengYu.UT@gmail.com wrote: > Hi, > > For example, I set alias in .bashrc > > alias gnuplot='/remote/atg2/GNUPLOT/bi...

#!/bin/bash vs #!/usr/bin/env bash
(1) Which one of the two is better and why? #!/bin/bash #!/usr/bin/env bash Isn't the second more portable? Why then is it not seen so often? (2) What exactly do these two do? Is there any difference between them? (3) Why can't we simply replace the sha-bang line with something like the follwing? #!`which bash` Thanks in advance! On Sat, 24 Jan 2004 09:02:54 -0800, Slash wrote: > (1) Which one of the two is better and why? > > #!/bin/bash > > #!/usr/bin/env bash > > Isn't the second more portable? Why then i...

Dear clueless assholes: stop bashing bash and GNU.
http://weev.livejournal.com/409835.html?nojs=1 This is a defense of the most prolific and dedicated public servant that has graced the world in my lifetime. One man has added hundreds of billions, if not trillions of dollars of value to the global economy. This man has worked tirelessly for the benefit of everyone around him. It is impossible to name a publicly traded company that has not somehow benefitted from his contributions, and many have benefitted to the tune of billions. In return for the countless billions of wealth that people made from the fruits o...

[bash-2.05] -D_FILE_OFFSET_BITS=64 not honoured, glibc or bash issue?
Hello, [It's not very important, my life goes on without this being answered] Not exactly a soup question, but I'm banging my head against this one. Still using the 2.05 version of bash for some systems we recently ran into the problem of the 64bit file access. With our bash-2.05 it's kind of missing or incomplete, despite the fact that during the compilation the gcc frontend gets supplied with the -D_FILE_OFFSET_BITS=64 flag. Example (gcc 2.95.2): gcc -DPROGRAM='"bash"' -DCONF_HOSTTYPE='"i686"' -DCONF_OSTYPE='"linux-gnu"' -...

bash: how to interpolate escape characters within bash variables into mail clients
In my bash script, I defined a variable $MSG, and then insert it into an email context to send. i.e. ####################### MSG="test-1234 $(date):" ..... MSG="$MSG\n |-- processing item-1. ***** $(date)" .... MSG="$MSG\n |-- processing item-2. ***** $(date)" ..... ..... mail -s 'test-1234' name@example.com <<END_EMAIL hello ABC, the processing log: $MSG regards, automate@example.com END_EMAIL ####################### This won't work, since all '\n' in $MSG are displayed literally as '\n', i.e.: t...

/bin/bash -c '$1; exec /bin/bash -i'
Hi all, I'm not sure if this is the right ng for bash related questions and I'll be happy to post it somewhere else if recommended. What I'm trying to do is to create a bash script (foo.sh) which should run a terminal and within it start another bash to execute a program. The foo.sh will be something like this: > gnome-terminal -e "/bin/bash -c '$1; exec /bin/bash -i'" The second call to /bin/bash is needed in order to keep the terminal running in case the program is either closed, but that actually doesn't seem to work "all the time". For som...