escape sequences in string variables
is it possible for a variable to contain escape sequences, specifically
newline ?
these commands:
echo -e "line 1\nline 2"
a=`echo -e "line 1\nline 2"`
echo $a
give output:
line 1
line 2
line 1 line 2
the newline in variable gets replaced with a space (using bash)
Re: escape sequences in string variables
In article <opswg6nkxpiafrhz [at] q-0ipvqbesgeo8i>, yyy [at] yyy.yyy wrote:
> is it possible for a variable to contain escape sequences, specifically
> newline ?
>
> these commands:
>
> echo -e "line 1\nline 2"
> a=`echo -e "line 1\nline 2"`
> echo $a
>
> give output:
>
> line 1
> line 2
> line 1 line 2
>
> the newline in variable gets replaced with a space (using bash)
Try: echo "$a"
--
Barry Margolin, barmar [at] alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Re: escape sequences in string variables
On 2005-09-02, yyy [at] yyy.yyy <yyy [at] yyy.yyy> wrote:
> is it possible for a variable to contain escape sequences, specifically
> newline ?
Yes.
> these commands:
>
> echo -e "line 1\nline 2"
> a=`echo -e "line 1\nline 2"`
> echo $a
>
> give output:
>
> line 1
> line 2
> line 1 line 2
>
> the newline in variable gets replaced with a space (using bash)
The shell performs word splitting on the result of variable
expansion. Since newline _is_ a word separator, echo sees two
arguments : "line 1" and "line 2".
Put quotes around the variable reference to prevent it.
echo "$a"
--
André Majorel <URL:http://www.teaser.fr/~amajorel/>
(Counterfeit: ejuqofib [at] affluence.org wer [at] verlag.com)
"La presse doit diffuser des idées saines." -- Serge Dassault,
propriétaire de la Socpresse.
Re: escape sequences in string variables
Andre Majorel wrote:
> On 2005-09-02, yyy [at] yyy.yyy <yyy [at] yyy.yyy> wrote:
>
>
>>is it possible for a variable to contain escape sequences, specifically
>>newline ?
>
>
> Yes.
>
>
>>these commands:
>>
>> echo -e "line 1\nline 2"
>> a=`echo -e "line 1\nline 2"`
>> echo $a
>>
>>give output:
>>
>> line 1
>> line 2
>> line 1 line 2
>>
>>the newline in variable gets replaced with a space (using bash)
>
>
> The shell performs word splitting on the result of variable
> expansion. Since newline _is_ a word separator, echo sees two
> arguments : "line 1" and "line 2".
ITYM 4 arguments: line, 1, line, and 2.
Ed.
Re: escape sequences in string variables
On 2005-09-02, Ed Morton <morton [at] lsupcaemnt.com> wrote:
> Andre Majorel wrote:
>> On 2005-09-02, yyy [at] yyy.yyy <yyy [at] yyy.yyy> wrote:
>>
>>
>>>is it possible for a variable to contain escape sequences, specifically
>>>newline ?
>>
>>
>> Yes.
>>
>>
>>>these commands:
>>>
>>> echo -e "line 1\nline 2"
>>> a=`echo -e "line 1\nline 2"`
>>> echo $a
>>>
>>>give output:
>>>
>>> line 1
>>> line 2
>>> line 1 line 2
>>>
>>>the newline in variable gets replaced with a space (using bash)
>>
>>
>> The shell performs word splitting on the result of variable
>> expansion. Since newline _is_ a word separator, echo sees two
>> arguments : "line 1" and "line 2".
>
> ITYM 4 arguments: line, 1, line, and 2.
Yes of course. Brain myopia, sorry.
--
André Majorel <URL:http://www.teaser.fr/~amajorel/>
(Counterfeit: uhej [at] committee.com imib [at] solvent.com)
"La presse doit diffuser des idées saines." -- Serge Dassault,
propriétaire de la Socpresse.
Re: escape sequences in string variables
thanks. it works. but why ?
it seems that somewhere the variable gets unnecessarily parsed, but where
exactly ? are variables not typed and atomic ?
Re: escape sequences in string variables
yyy [at] yyy.yyy wrote:
>
> thanks. it works. but why ?
What worked? Please quote enough of the post you're resonding to to give
some context.
> it seems that somewhere the variable gets unnecessarily parsed, but
> where exactly ? are variables not typed and atomic ?
If you're talking about your original question, the answer was just to
quote your variable when you echo it. See this thread for an explanation
of why that's advisable: http://tinyurl.com/dqu5g
Regards,
Ed.
Re: escape sequences in string variables
On Fri, 02 Sep 2005 12:06:07 -0500, Ed Morton <morton [at] lsupcaemnt.com>
wrote:
> yyy [at] yyy.yyy wrote:
>> thanks. it works. but why ?
>
> What worked? Please quote enough of the post you're resonding to to give
> some context.
>
>> it seems that somewhere the variable gets unnecessarily parsed, but
>> where exactly ? are variables not typed and atomic ?
>
> If you're talking about your original question, the answer was just to
> quote your variable when you echo it. See this thread for an explanation
> of why that's advisable: http://tinyurl.com/dqu5g
>
> Regards,
>
> Ed.
i've developed some bad, as it seems, habits of not quoting, not only
shell variables
yes, the given answers were perfectly right, but did not state why they
were correct, something that could hardly help avoid mistakes in future,
the article at the url you've supplied gave some more insight
thanks for the help
Re: escape sequences in string variables
In article <opswhj81bkiafrhz [at] q-0ipvqbesgeo8i>, yyy [at] yyy.yyy wrote:
> yes, the given answers were perfectly right, but did not state why they
> were correct, something that could hardly help avoid mistakes in future,
> the article at the url you've supplied gave some more insight
I guess we expected you to do your own research to discover why. You'll
learn better that way.
--
Barry Margolin, barmar [at] alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***