Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!usc!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!aplcen!uunet!mcsun!hp4nl!star.cs.vu.nl!maart From: maart@cs.vu.nl (Maarten Litmaath) Newsgroups: comp.unix.questions Subject: Re: sh(1) question from a "quoting paranoiac" :-) Message-ID: <5941@star.cs.vu.nl> Date: 8 Mar 90 19:29:41 GMT References: <2387@rodan.acs.syr.edu> Sender: news@cs.vu.nl Reply-To: maart@cs.vu.nl (Maarten Litmaath) Organization: VU Informatika, Amsterdam, the Netherlands Lines: 62 In article <2387@rodan.acs.syr.edu>, jdpeek@rodan.acs.syr.edu (Jerry Peek) writes: )... ) last="`expr \"$*\" : '.* \(.*\)'`" # LAST ARGUMENT ) first="`expr \"$*\" : '\(.*\) .*'`" # ALL BUT LAST ARG ) )Seems like, years ago, I needed to put doublequotes around the backquotes )to protect any spaces from the shell. [...] Indeed. Let's assume the IFS characters are space, tab and newline, and no argument contains such characters; then you could remove the outer double quotes. Example: $ set a b c $ last=`expr "$*" : '.* \(.*\)'` $ first=`expr "$*" : '\(.*\) .*'` $ echo "=$first=$last=" =a b=c= This should work too (more consistent, IMHO): $ last=`expr "$*" : '.* \\(.*\\)'` Right, let's try some funny arguments: $ set a ' ' b $ last=`expr "$*" : '.* \(.*\)'` $ first=`expr "$*" : '\(.*\) .*'` $ echo "=$first=$last=" =a =b= $ /bin/echo `expr "$*" : '\(.*\) .*'` | od -a 0000000 a nl 0000002 Huh?! In an assignment the results of command substitution are NOT scanned for IFS characters! (At least on SunOS 4.0.3c and 4.3BSD.) All the SunOS manual has to say about assignments: Keyword parameters (also known as variables) may be assigned values by writing: name=value [ name=value ] ... Pattern-matching is not performed on value. So: $ a=* $ echo "$a" * Anyway, here's an example showing the effect of an embedded space in the last argument: $ set a b c' 'd $ last=`expr "$*" : '.* \(.*\)'` $ first=`expr "$*" : '\(.*\) .*'` $ echo "=$first=$last=" =a b c=d= -- "Belfast: a sentimental journey to the Dark Ages - Crusades & Witchburning - Europe's Lebanon - Book Now!" | maart@cs.vu.nl, uunet!mcsun!botter!maart