Getting at the first char of a string in Bourne shell

David Smallberg das at oahu.cs.ucla.edu
Tue Oct 2 06:13:34 AEST 1990


In article <1990Sep29.215559.25098 at cti-software.nl> pim at cti-software.nl (Pim Zandbergen) writes:
>How about 
>In Korn shell you can use a hack like
>
>string=${string%${string#?}}

A fixed-length variable also does the trick:
	typeset -L1 first="$string"
	rest=${string#$first}
Unfortunately, a non- or not clearly documented feature of left- (right-)
justified variables is that leading (trailing) whitespace is stripped off
before the truncating assignment is made, so that
	string="   abc"
	typeset -L1 first="$string"
	echo ">$first<"
yields
	>a<
instead of
	> <
--

-- David Smallberg, das at cs.ucla.edu, ...!{uunet,ucbvax,rutgers}!cs.ucla.edu!das



More information about the Comp.unix.shell mailing list