Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Determing full path of shell script (from inside the script)

105 views
Skip to first unread message

Kenny McCormack

unread,
Feb 20, 2013, 7:20:35 AM2/20/13
to
Suppose I am in a shell script and I want to know the full path to the
script. Ignore, for the time being, the usual noises about how this is a
bad idea, you shouldn't want or need to do this, etc, etc.

Note that, under Linux, I can use /proc/self/exe to get the full path to the
interpreter (/bin/bash, or whatever), but I am interested in the script
itself.

Feel free to answer this under any or all of the following conditions (or
any other that you can think of...):

1) Portable (I.e., works on any normal system/shell, even on systems where
/bin/sh isn't a "standard" shell [e.g., Solaris])

2) POSIX (I.e., assume a "standard" shell)

3) Bash

4) Zsh

5) Linux (I.e., you can take advantage of Linux-specific things, e.g., /proc)

--
"The anti-regulation business ethos is based on the charmingly naive notion
that people will not do unspeakable things for money." - Dana Carpender

Quoted by Paul Ciszek (pciszek at panix dot com). But what I want to know
is why is this diet/low-carb food author doing making pithy political/economic
statements?

Nevertheless, the above quote is dead-on, because, the thing is - business
in one breath tells us they don't need to be regulated (which is to say:
that they can morally self-regulate), then in the next breath tells us that
corporations are amoral entities which have no obligations to anyone except
their officers and shareholders, then in the next breath they tell us they
don't need to be regulated (that they can morally self-regulate) ...

pk

unread,
Feb 20, 2013, 7:39:09 AM2/20/13
to
On Wed, 20 Feb 2013 12:20:35 +0000 (UTC), gaz...@shell.xmission.com (Kenny
McCormack) wrote:

> Suppose I am in a shell script and I want to know the full path to the
> script. Ignore, for the time being, the usual noises about how this is a
> bad idea, you shouldn't want or need to do this, etc, etc.
>
> Note that, under Linux, I can use /proc/self/exe to get the full path to
> the interpreter (/bin/bash, or whatever), but I am interested in the
> script itself.
>
> Feel free to answer this under any or all of the following conditions (or
> any other that you can think of...):
>
> 1) Portable (I.e., works on any normal system/shell, even on systems where
> /bin/sh isn't a "standard" shell [e.g., Solaris])
>
> 2) POSIX (I.e., assume a "standard" shell)
>
> 3) Bash
>
> 4) Zsh
>
> 5) Linux (I.e., you can take advantage of Linux-specific things,
> e.g., /proc)

If you skip the "it's a bad idea", "don't do this" etc. parts, there's a
good amount of information here:

http://mywiki.wooledge.org/BashFAQ/028


Heinz Müller

unread,
Feb 20, 2013, 12:39:22 PM2/20/13
to
Am 20.02.2013 13:20, schrieb Kenny McCormack:
> Suppose I am in a shell script and I want to know the full path to the
> script. Ignore, for the time being, the usual noises about how this is a
> bad idea, you shouldn't want or need to do this, etc, etc.
>
> Note that, under Linux, I can use /proc/self/exe to get the full path to the
> interpreter (/bin/bash, or whatever), but I am interested in the script
> itself.
>
> Feel free to answer this under any or all of the following conditions (or
> any other that you can think of...):
>
> 1) Portable (I.e., works on any normal system/shell, even on systems where
> /bin/sh isn't a "standard" shell [e.g., Solaris])
>
> 2) POSIX (I.e., assume a "standard" shell)
>
> 3) Bash
>
> 4) Zsh
>
> 5) Linux (I.e., you can take advantage of Linux-specific things, e.g., /proc)
>
Hi,

in all my scripts I do something like:

#
# name of the script without suffix, e.g. .ksh or .sh
SCRIPT=${0##*/}
SCRIPTNAME=${SCRIPT%.*}

#
# set startdir
cd "`dirname \"$0\"`/../"
STARTDIR="`pwd`"

LOGDIR=$STARTDIR/log
LOGFILE=$LOGDIR/$SCRIPTNAME.log
SCRIPTDIR=$STARTDIR/src
TMPDIR=$STARTDIR/tmp


So if my directory is

/etc/scripts/Dummy

with the subdirs

src
log
tmp

my script is placed in src

src/dummy.ksh

and all variables are filled automatically.

UNIX: Solaris 10

Heinz

Jon LaBadie

unread,
Feb 20, 2013, 6:03:13 PM2/20/13
to
On 02/20/2013 07:20 AM, Kenny McCormack wrote:
> Suppose I am in a shell script and I want to know the full path to the
> script. Ignore, for the time being, the usual noises about how this is a
> bad idea, you shouldn't want or need to do this, etc, etc.
>
> Note that, under Linux, I can use /proc/self/exe to get the full path to the
> interpreter (/bin/bash, or whatever), but I am interested in the script
> itself.
>
> Feel free to answer this under any or all of the following conditions (or
> any other that you can think of...):
>
> 1) Portable (I.e., works on any normal system/shell, even on systems where
> /bin/sh isn't a "standard" shell [e.g., Solaris])
>
> 2) POSIX (I.e., assume a "standard" shell)
>
> 3) Bash
>
> 4) Zsh
>
> 5) Linux (I.e., you can take advantage of Linux-specific things, e.g., /proc)
>
on my system lsof -p $$ has the full pathname to the script among its open files.

And /proc/$$/fd/* contains a symbolic link to the full path.

While it is only one data point, it was true for ksh and bash whether invoked
as myscript, shell myscript, or shell < myscript.

Kenny McCormack

unread,
Feb 20, 2013, 6:32:46 PM2/20/13
to
In article <kg3klp$ptt$1...@dont-email.me>,
Jon LaBadie <jlab...@aXcXm.org> wrote:
...
>on my system lsof -p $$ has the full pathname to the script among its
>open files.
>
>And /proc/$$/fd/* contains a symbolic link to the full path.
>
>While it is only one data point, it was true for ksh and bash whether invoked
>as myscript, shell myscript, or shell < myscript.

Indeed. I had noticed that bash keeps it open on fd 255 - so, if you are
willing to assume Linux & Bash, then: readlink /proc/self/fd/255
brings home the bacon.

This fact is also mentioned on the page referenced by pk.

--
"We should always be disposed to believe that which appears to us to be
white is really black, if the hierarchy of the church so decides."

- Saint Ignatius Loyola (1491-1556) Founder of the Jesuit Order -

Stephane Chazelas

unread,
Feb 20, 2013, 6:51:57 PM2/20/13
to
2013-02-20 12:20:35 +0000, Kenny McCormack:
> Suppose I am in a shell script and I want to know the full path to the
> script. Ignore, for the time being, the usual noises about how this is a
> bad idea, you shouldn't want or need to do this, etc, etc.
[...]

Generally

script=$(cd -P -- "$(dirname -- "$0")" && pwd) &&
script=$script/$(basename -- "$0")

is good enough (note that though the symlinks are resolved in
the components of the dirname, that path itself may be a symlink).

With zsh:

script=$0:a

Or script=$0:A to resolve symlinks


Cases where it won't work properly are like if the script was
called as

sh the-script

and the-script is looked up in $PATH (that is, is not found in
the current directory) or if the script name or its dirname end
in newline characters.

--
Stephane
0 new messages