bash: Where Am I?

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
Steve
5/20/2006 1:03:00 PM
comp.unix.shell 15269 articles. 0 followers. Post Follow

5 Replies
536 Views

Similar Articles

[PageSpeed] 55
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
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
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
Barry
5/20/2006 10:49:14 PM
curdir=$(cd `dirname $0`; pwd)

0
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
Bill
5/22/2006 12:52:07 AM
Reply:
Similar Artilces:

Build problem using Cygwin bash shell for VxWorks 5.5
To: ace-bugs@cs.wustl.edu Subject: [area]: [synopsis] ACE VERSION: 5.5 HOST MACHINE and OPERATING SYSTEM: Windows 2K TARGET MACHINE and OPERATING SYSTEM, if different from HOST: x86 Simulator, VxWorks 5.5 COMPILER NAME AND VERSION (AND PATCHLEVEL): Whatever GNU compiler ships with Tornado 2.2.1 CONTENTS OF $ACE_ROOT/ace/config.h: #if defined (_MSC_VER) || defined (__BORLANDC__) # include "ace/config-win32.h" # undef ACE_HAS_EXCEPTIONS #else # include "ace/config-vxworks5.x.h" #endif CONTENTS OF $ACE_ROOT/include/makeinclude/platform_macros....

Running bash inside emacs
Hi I was wondering if it is possible to run a bash shell inside an emacs buffer (where the up and down keys recall commands executed in .bash_history and do not move the cursor around -- the problem with the native shell available in emacs, tab results in command completion, etc.). Thanks. >>>>> Madhusudan Singh <spammers-go-here@spam.invalid> writes: > Hi > I was wondering if it is possible to run a bash shell inside an emacs > buffer (where the up and down keys recall commands executed > in .bash_history and do not move the cursor around -- the problem ...

up-arrow for last command in bash?
I've been trying to figure out how to get the last command to display on the command prompt in bash as opposed to the one I get when I log on as root, like if ( $?tcsh ) then bindkey "^W" backward-delete-word bindkey -k up history-search-backward bindkey -k down history-search-forward endif in my .cshrc in root? when I'm logged as a normal user (bash), I can't get bindkey to work. Probably cause I have no idea what I'm doing when it comes to shell stuff. jeff. -- --- Jeff D. Ham...

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 subshell question
I am trying to process the output of a command that produces multi-line output. I am piping the output of the command into a while loop and parsing each line of output. However, since this 'launches' the loop in a subshell, I am not able to get at the results of my parsing outside the loop. A short script representing what I am trying to accomplish is presented below; I am wondering what solutions are commonly employed for this problem? It seems rather inelegant to store the results in an intermediate file .... is this really the only way? I could actually get by with taking actio...

(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/ -...

Input from PBS bash file into matlab
Hi, I have a simple question but I can't find any documentation anywhere. I'd like to write a bash script which request a certain number of nodes in a look, then according to the node number, the number is then in-putted as an argument in a matlab function and the matlab function is run. The real trouble I have is that I have to use scripts, because I have to use the deploy tool and the deploy tool only works with scripts. So somehow my script which I have already compiled using the compile tool should be able to read the pbs output. I dont believe this is possible which is a...

Working with bash and cat
I just downloaded bsh204br3.zip from current but when executing the following script I get an error "Bad file descriptor (EBADF)": echo 1 | cat I tried downloading the latest beta bsh205bbr3.zip but then whenever I pipe something into cat, nothing gets output to the screen. Is there a more recent version of bash I can download which fixes these problems? Matt D. Bash pipe doesn't work at all with any of the 2.05 binaries. All of these fail: sh.exe sh-2.05$ echo 1 | cat sh-2.05$ echo 1 | while read a; do echo $a; done sh-2.05$ echo 1 | (read a &...

bash: javac: command not found problem
Hi: I am new to Linux/unix. I was trying to compile a simple hello world java program in my local and a remote (after ssh from my local machine) linux machines, but both i got "bash: javac: command not found" errors. When i used command "java -version" on both machines, I got "java version 1.6.0_02, java TM/SE Runtime Environment (build 1.6.0_02.605), Java HotSpot(TM) client VM ....." on my local PC, and got "java version 1.4.2, gcj (GCC) 3.4.6.20060404 (Red Hat 3.4.6-3) ......" on the remote server. When i used command "find / - name javac" t...

crontab, command substitution and bash
I have the a crontab file that will not run, with the following error log: Subject: Cron <brendan@saglek> $IOFS_SCRIPT_DIR/run_iofs.bash $(date + X-Cron-Env: <SHELL=/bin/bash> X-Cron-Env: <IOFS_SCRIPT_DIR=/home/brendan/iofs_v0.1/script/bash> X-Cron-Env: <HOME=/home/brendan> X-Cron-Env: <PATH=/usr/bin:/bin> X-Cron-Env: <LOGNAME=brendan> X-Cron-Env: <USER=brendan> Date: Tue, 24 Apr 2007 21:00:01 -0300 (ADT) /bin/bash: -c: line 0: unexpected EOF while looking for matching `)' /bin/bash: -c: line 1: syntax error: unexpected end of file Crontab seems ...

[gnu.bash.bug] BASH Frequently-Asked Questions (FAQ version 3.34) #2
Archive-name: unix-faq/shell/bash Posting-Frequency: monthly Submitted-By: chet@po.cwru.edu (Chet Ramey) Last-Modified: Tue Oct 10 10:15:38 EDT 2006 FAQ-Version: 3.34 Bash-Version: 3.2 URL: ftp://ftp.cwru.edu/pub/bash/FAQ Maintainer: chet@po.cwru.edu (Chet Ramey) This is the Bash FAQ, version 3.34, for Bash version 3.2. This document contains a set of frequently-asked questions concerning Bash, the GNU Bourne-Again Shell. Bash is a freely-available command interpreter with advanced features for both interactive use and shell programming. Another good source of basic information about shell...

pipe perl output to a bash command error
I was wondering why the first line give me output but the second don't, any clue!! 1. while sleep 2;do printf "1\n1\n"|perl -e 'while ( <> ) { print; }';done|perl -e 'while ( <> ) { print; }' 2. while sleep 2;do printf "1\n1\n"|perl -e 'while ( <> ) { print; }';done|perl -e 'while ( <> ) { print; }'|cat Tarek Elganainy wrote: > I was wondering why the first line give me output but the second > don't, any clue!! > > 1. while sleep 2;do printf "1\n1\n"|perl -e 'while ( <> ) >...

ps not listing child process started by bash
Hi, I ran the following infinite loop on 2.6.9-5.EL ( Redhat Hat Linux ) system while true; do echo "hello" >> /parjai/testing_file; done & output of ps is not showing that such command is running or not. Is it the expected behaviour?? In multiuser environment ( specially for a developer team) it would be very difficuilt to find such culprit processes. Is there any way to identify which process is eating more space on a partition. Thanks eyemole eyemole wrote: > I ran the following infinite loop on 2.6.9-5.EL ( Redhat Hat Linux ) > system > while true; do...

Getting Bash\XTerm to show the full path.
I recently migrated from Linux to FreeBSD. Everything so far is great, the only thing that is bugging me is the fact that i do not see the fully qualified path of my current working directory when using an "Xterm" (or even on the console w/o any UI). What i see is: bash-2.05$ And that is my entire prompt. What i would like to see is: /usr/home/foo/bar> or similar, like it was on Linux. I wasn't sure if this was a new Bash issue, or if it's the FreeBSD version specifically that enforces it, but for me personally it really creates headaches when i'm tr...

[eject problem] need help with bash script #2
(sorry in advance if google starts a new thread :-/ ) >But you changed the script to "Eject $1 failed, error is $?." Does the >script not give you the error number? If not, then perhaps you didn't >change what you think you changed. If so, what is the error number given? Yes, it gives error 1 (whatever that means). Forgot to mention it. >Can you run eject from the command line? What happens? Yes, that's what made me try !#/bin/bash instead of !#/bin/sh in the script. From the command line (in the bash shell) the command works, but the same command in the !#/b...

BASH Command Query?!?!
I am trying to batch process a bunch of camera photo files and have a working command but I'm trying to refine it. I have a series of photo files with the following syntax: crw_1270.crw crw_1271.crw I have written a script to convert to a low res jpeg: for i in *.crw; do dcraw -w -c $i | cjpeg > $i.lo.jpg; echo $i; done crw_1270.crw crw_1270.crw.lo.jpg crw_1271.crw crw_1271.crw.lo.jpg But, I'm trying to shorten the resulting output file name a bit. I've tried the following command (watch for the line wrap): for i in *.crw; j=`echo $i | cut -d "." -f 1`; do dcraw...

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 ...

How To Spice Up Menus in Bash
Anyone out there use Suse's Yast tool from command line? I want to create some simple command line tools for adding users into a database, but want to spice it up a little like Yast. Moreover, I want to keep this really, really simple by using Bash. Is there a program I can download that lets me create scripts in Bash to give me a Yast-like experience? Also, you Novell-heads out there (said nicely -- I once was a CNE) you may remember the Novell command line text GUI (TUI) environment that is similar to Yast. Anyone got an answer? Much appreciated. Thanks. In comp.unix.shell googlemike@...

how to store strings in variables with awk, bash?
hello, I have a file.txt with following content: ---------------------------file.txt------------------------------ 109 69 47 46 53 49 79 83 -------------------------------------------------------------------- ----------------------------myscript.sh------------------------- #!/bin/bash X=0 while [ $X -le $rlc_blocks ] #while [ $X -lt 3 ] do X=$((X+1)) {myvariable[$X]} = $(mawk '{print $3}' file.txt) done ------------------------------------------------------------------------ Can anybody help me with this script? How do I store individual numbers from different lines of file.txt i...

[gnu.bash.bug] BASH Frequently-Asked Questions (FAQ version 3.26)
Archive-name: unix-faq/shell/bash Posting-Frequency: monthly Submitted-By: chet@po.cwru.edu (Chet Ramey) Last-Modified: Mon Aug 18 13:59:33 EDT 2003 FAQ-Version: 3.26 Bash-Version: 2.05b URL: ftp://ftp.cwru.edu/pub/bash/FAQ Maintainer: chet@po.cwru.edu (Chet Ramey) This is the Bash FAQ, version 3.26, for Bash version 2.05b. This document contains a set of frequently-asked questions concerning Bash, the GNU Bourne-Again Shell. Bash is a freely-available command interpreter with advanced features for both interactive use and shell programming. Another good source of basic information about s...

Double quotation in bash
This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enig2EF0A62F9C499CF658CD5B95 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Hi group, I've got a question. When there are two bash scripts: # cat test1 #!/bin/sh moo='this "oh how beautiful" is a test' ../test2 $moo #cat test2 #!/bin/sh echo "1 = $1" echo "2 = $2" echo "3 = $3" echo "4 = $4" When I call #./test1 I get the following output: 1 = this 2 = "oh 3 = how 4 = beautiful" However, I'd like to have...

Need bash "Greater Than" help
Hi All, I have been using this bash "if" statement if [ "$LatestRev" \> "$OldRev" ]; then with success, until this happened: LatestRev=Firefox Setup 3.6.10.exe OldRev=Firefox Setup 3.6.9.exe Any advice on how to make "3.6.10" look larger that "3.6.9"? Many thanks, -T On 2010-09-19, Todd wrote: > Hi All, > > I have been using this bash "if" statement > > if [ "$LatestRev" \> "$OldRev" ]; then > > with success, until this happened: > > ...

rsync keeps failing from a bash/expect script
Hello, I have a script that I use to run periodic updates of backups on to a remote server. Its a bash script, but it contains two expect blocks (the rsync command is immediately after the spawn directive) : VAR1=$(expect -c " spawn rsync -azuvWh --stats --exclude-from=$excludefile --include-from=$includefile --log-file $rsynclogfile $username@$remotehost:$remoteroot/* $localroot expect \"password:\" send \"$passwd\r\n\" expect \"speedup\" send \"\r\n\" ") VAR2=$...

parsing-bash shell-awk
Hi People: Could anybody tell me how can I do this with awk or bash shell? I've a file with this systaxis: #cat file I went fishing for some sea <bass>. The <bass> part of the song is very moving. he proposed an elaborate <program> of public works.This information was taken from the magazine. the <program> required several hundred lines of code. I need a script that gets 3 previus words and 3 words after the tag(<>) from a text file,EXCEPT the words greater than 2 chars length(example of,a,an). Returning something like this: #cat results for some sea ba...

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 ...