Re: Command substitution / word splitting question
From:
pk
Subject:
Re: Command substitution / word splitting question
Date:
Fri, 23 Apr 2010 09:39:02 +0100
User-agent:
KNode/4.3.5
Binarus wrote:
> The script
>
> echo 'line1' > file
> echo 'line2' >> file
> RESULT=$(cat file)
> echo "$RESULT"
>
> gives the following output:
>
> line1
> line2
>
> I don't understand why: the command substitution $(cat file) is not
> within double quotes, so word splitting should be applied to the
> result of the substitution and any embedded newlines should be
> removed. Thus, $RESULT should contain text without newlines after line
> three in the above script is executed.
Word splitting does not happen in variable assignment. So
RESULT=$(cat file)
puts literally
line1
line2
in RESULT. When you later do echo "$RESULT", quoting the variable preserves
its value.