From owner-freebsd-questions@FreeBSD.ORG Wed Nov 14 15:19:13 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 CECDE16A417 for ; Wed, 14 Nov 2007 15:19:13 +0000 (UTC) (envelope-from erikt@midgard.homeip.net) Received: from ch-smtp02.sth.basefarm.net (ch-smtp02.sth.basefarm.net [80.76.149.213]) by mx1.freebsd.org (Postfix) with ESMTP id 82BB713C45A for ; Wed, 14 Nov 2007 15:19:12 +0000 (UTC) (envelope-from erikt@midgard.homeip.net) Received: from c83-253-25-183.bredband.comhem.se ([83.253.25.183]:64662 helo=falcon.midgard.homeip.net) by ch-smtp02.sth.basefarm.net with esmtp (Exim 4.68) (envelope-from ) id 1IsK0b-0007Ac-7z for freebsd-questions@freebsd.org; Wed, 14 Nov 2007 16:18:29 +0100 Received: (qmail 38102 invoked from network); 14 Nov 2007 16:18:26 +0100 Received: from owl.midgard.homeip.net (10.1.5.7) by falcon.midgard.homeip.net with ESMTP; 14 Nov 2007 16:18:26 +0100 Received: (qmail 51971 invoked by uid 1001); 14 Nov 2007 16:18:26 +0100 Date: Wed, 14 Nov 2007 16:18:26 +0100 From: Erik Trulsson To: Martin McCormick Message-ID: <20071114151826.GA51943@owl.midgard.homeip.net> Mail-Followup-To: Martin McCormick , freebsd-questions@freebsd.org References: <200711141508.lAEF8veZ083725@m.it.okstate.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <200711141508.lAEF8veZ083725@m.it.okstate.edu> User-Agent: Mutt/1.5.16 (2007-06-09) X-Originating-IP: 83.253.25.183 X-Scan-Result: No virus found in message 1IsK0b-0007Ac-7z. X-Scan-Signature: ch-smtp02.sth.basefarm.net 1IsK0b-0007Ac-7z 63a10f18f9db9a26cdb3a946ad00b9fa 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 15:19:14 -0000 On Wed, Nov 14, 2007 at 09:08:57AM -0600, Martin McCormick wrote: > 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=`echo $0 |awk 'BEGIN{FS="/"}{print $NF}'` > echo $PROGNAME > > That beautifully isolates the script name but if you happen to > call the script without prepending a path name such as when the > script is in the execution path, you get an error because there > are no slashes in the string so awk gets confused. > > Is there a better way to always end up with only the script name and > nothing else no matter whether the path was prepended or not? > The basename(1) command seems to do what you want. -- Erik Trulsson ertr1013@student.uu.se