Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!elroy.jpl.nasa.gov!ncar!gatech!emory!hubcap!ncrcae!ncrlnk!ncrstp!npdiss1!mercer From: mercer@npdiss1.StPaul.NCR.COM (Dan Mercer) Newsgroups: comp.unix.shell Subject: Re: Finding the last arg Keywords: Bourne shell arguments Message-ID: <811@npdiss1.StPaul.NCR.COM> Date: 1 Jan 91 23:42:51 GMT References: <18476@shlump.nac.dec.com> <1990Dec27.154917.14751@virtech.uucp> Reply-To: mercer@npdiss1.StPaul.NCR.COM (Dan Mercer) Organization: StPaul Lines: 42 In article <1990Dec27.154917.14751@virtech.uucp> cpcahil@virtech.UUCP (Conor P. Cahill) writes: :In article <18476@shlump.nac.dec.com> lan_csse@netrix.nac.dec.com (CSSE LAN Test Account) writes: :>The obvious thing to try is some sort of expression combining $# with ${}, :>e.g. ${$#}. This gets a syntax error. The simpler $$# is valid, but it : :Try: : eval \$$# : :-- :Conor P. Cahill (703)430-9247 Virtual Technologies, Inc., :uunet!virtech!cpcahil 46030 Manekin Plaza, Suite 160 : Sterling, VA 22170 Obviously this fails for $# > 9. So you need some form of shift, preferably forking and execing as few processes as possible (nice to avoid expr). You'd also like to preserve the current args. So try this: args="$@" # get arglist argno=$# # and number of current args export args argno last=`set -- spacer $args # set as args - add spacer to eliminate expr shift $argno # shift over echo $1` This works if args contains no args with embedded whitespace - if that is a possibility, then expr must be used. args="$@" export args argno last=` set -- spacer $args shift \`expr $# - 1\` echo $1` echo $last But this also fails if last arg may contain whitespace. Oh well! -- Dan Mercer NCR Network Products Division - Network Integration Services Reply-To: mercer@npdiss1.StPaul.NCR.COM (Dan Mercer) "MAN - the only one word oxymoron in the English Language"