From owner-freebsd-questions@FreeBSD.ORG Wed Nov 14 18:13:31 2007 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 7DFD716A481 for ; Wed, 14 Nov 2007 18:13:31 +0000 (UTC) (envelope-from michael.grunewald@laposte.net) Received: from postfix2-g20.free.fr (postfix2-g20.free.fr [212.27.60.43]) by mx1.freebsd.org (Postfix) with ESMTP id 353AC13C521 for ; Wed, 14 Nov 2007 18:13:31 +0000 (UTC) (envelope-from michael.grunewald@laposte.net) Received: from smtp5-g19.free.fr (smtp5-g19.free.fr [212.27.42.35]) by postfix2-g20.free.fr (Postfix) with ESMTP id 0871F1E9FFC2 for ; Wed, 14 Nov 2007 16:46:52 +0100 (CET) Received: from smtp5-g19.free.fr (localhost.localdomain [127.0.0.1]) by smtp5-g19.free.fr (Postfix) with ESMTP id 492D73F61B3; Wed, 14 Nov 2007 18:47:42 +0100 (CET) Received: from Llea.celt.neu (ron34-3-82-236-236-194.fbx.proxad.net [82.236.236.194]) by smtp5-g19.free.fr (Postfix) with ESMTP id A8CFA3F61AB; Wed, 14 Nov 2007 18:47:33 +0100 (CET) Received: from Llea.celt.neu (localhost [127.0.0.1]) by Llea.celt.neu (8.14.1/8.13.8) with ESMTP id lAEHqfhe001155; Wed, 14 Nov 2007 18:52:41 +0100 (CET) (envelope-from michael.grunewald@laposte.net) Received: (from michael@localhost) by Llea.celt.neu (8.14.1/8.13.8/Submit) id lAEHqeS4001154; Wed, 14 Nov 2007 18:52:40 +0100 (CET) (envelope-from michael.grunewald@laposte.net) X-Authentication-Warning: Llea.celt.neu: michael set sender to michael.grunewald@laposte.net using -f To: Martin McCormick References: <200711141508.lAEF8veZ083725@m.it.okstate.edu> From: michael.grunewald@laposte.net (=?iso-8859-15?Q?Micha=EBl_Gr=FCnewald?=) Date: Wed, 14 Nov 2007 18:52:40 +0100 In-Reply-To: <200711141508.lAEF8veZ083725@m.it.okstate.edu> (Martin McCormick's message of "Wed\, 14 Nov 2007 09\:08\:57 -0600") Message-ID: <864pfosoif.fsf@Llea.celt.neu> User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15 Content-Transfer-Encoding: quoted-printable Cc: freebsd-questions@freebsd.org Subject: Re: /bin/sh Can one Easily Strip Path Name from $0? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 14 Nov 2007 18:13:31 -0000 Martin McCormick 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