sed behaves differently when run in backquotes/subshell

Steve Rudek stever at tree.UUCP
Mon Aug 7 06:06:51 AEST 1989


I needed a shell script which would take strings of letters and alphabetize
them into a single line and sed + sort seemed like the best choice.  But I find 
that sed performs differently when part of a pipeline not explicitly in a
subshell than it does when run in backquotes.  What's the problem?

Given a file ".TST" containing:
e
s
m
l
o09
ux123
35
y8
anrt467
iwhp5z
h

and the following script:
DATA_DIR=.TST
#----------------------------#
echo "exploding and alphabetizing without subshell works fine"
cat .TST | sed 's/\(.\)/\1\
/g'|sort|paste -s -d"\0" -
#----------------------------#
echo "can't explode the strings when the same pipeline is run in backquotes"
GUESSES=`cat .TST | sed 's/\(.\)/\1\
/g'|sort|paste -s -d"\0" -`
echo "GUESSES==$GUESSES"

I get the output:
exploding and alphabetizing without subshell works fine
012334556789aehhilmnoprstuwxyz
can't explode the strings when the same pipeline is run in backquotes
GUESSES==35anrt467ehiwhp5zlmo09sux123y8

What is happening here is that strings such as "abc" are being properly
split into
a
b
c
in the first case while they pass through unchanged in the second case.
-- 
----------
Steve Rudek  {ucbvax!ucdavis!csusac OR ames!pacbell!sactoh0} !tree!stever



More information about the Comp.unix.questions mailing list