Date: Wed, 14 Nov 2007 18:52:40 +0100 From: michael.grunewald@laposte.net (=?iso-8859-15?Q?Micha=EBl_Gr=FCnewald?=) To: Martin McCormick <martin@dc.cis.okstate.edu> Cc: freebsd-questions@freebsd.org Subject: Re: /bin/sh Can one Easily Strip Path Name from $0? Message-ID: <864pfosoif.fsf@Llea.celt.neu> In-Reply-To: <200711141508.lAEF8veZ083725@m.it.okstate.edu> (Martin McCormick's message of "Wed\, 14 Nov 2007 09\:08\:57 -0600") References: <200711141508.lAEF8veZ083725@m.it.okstate.edu>
next in thread | previous in thread | raw e-mail | index | archive | help
Martin McCormick <martin@dc.cis.okstate.edu> writes:
> I am ashamed to admit that I have been writing shell
> scripts for about 15 years but this problem has me stumped. $0
> is the shell variable which contains the script name or at least
> what name is linked to the script. The string in $0 may or may
> not contain a path, depending upon how the script was called. It
> is easy to strip off the path if it is always there
>
> #! /bin/sh
> PROGNAME=3D`echo $0 |awk 'BEGIN{FS=3D"/"}{print $NF}'`
> echo $PROGNAME
As said by others, you can use `basename`. Apart from this, you can
fix your old habit by prepending a '/' before '$0', like this:
#! /bin/sh
PROGNAME=3D`echo /$0 |awk 'BEGIN{FS=3D"/"}{print $NF}'`
echo $PROGNAME
Last, you can also use variable expansions mechanisms by saying:
PROGNAME=3D"${0##*/}"
The main difference with `basename` way is that the latter do not call
a subprogram.
(If you are sure there is no space in your name, you can remove the
quotes, but are you sure?)
See `Parameter Expansion' in sh(1).
--=20
Best wishes,
Micha=EBl
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?864pfosoif.fsf>
