From owner-freebsd-questions@FreeBSD.ORG Wed Nov 14 15:19:18 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 8093116A49A for ; Wed, 14 Nov 2007 15:19:18 +0000 (UTC) (envelope-from wmoran@potentialtech.com) Received: from mail.potentialtech.com (internet.potentialtech.com [66.167.251.6]) by mx1.freebsd.org (Postfix) with ESMTP id 4FB0013C468 for ; Wed, 14 Nov 2007 15:19:18 +0000 (UTC) (envelope-from wmoran@potentialtech.com) Received: from vanquish.pitbpa0.priv.collaborativefusion.com (pr40.pitbpa0.pub.collaborativefusion.com [206.210.89.202]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.potentialtech.com (Postfix) with ESMTP id 2BCADEBC84; Wed, 14 Nov 2007 10:19:17 -0500 (EST) Date: Wed, 14 Nov 2007 10:19:16 -0500 From: Bill Moran To: Martin McCormick Message-Id: <20071114101916.c661f4ac.wmoran@potentialtech.com> In-Reply-To: <200711141508.lAEF8veZ083725@m.it.okstate.edu> References: <200711141508.lAEF8veZ083725@m.it.okstate.edu> X-Mailer: Sylpheed 2.4.4 (GTK+ 2.10.14; i386-portbld-freebsd6.2) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit 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:18 -0000 In response to Martin McCormick : > 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? basename $0 -- Bill Moran http://www.potentialtech.com