Problem: Command substitution adds an extra carriage return character on return.

Problem: Command substitution adds an extra carriage return character on return.

Post by Bo Jacobse » Sat, 15 Feb 2003 03:40:18



I have made a test script that shows the problem:

x=`echo -n "1234"`
y="1234"

echo -n $x > file1
echo -n $y > file2

After execution, file1 contains 5 characters; "1234"  AND a CR (Hex 0A).
file2 don't.

How do I avoid the extra CR when doing command substitution ?.

PS: I'm using the default shell (bash) in SuSE 8.1

Bo

 
 
 

Problem: Command substitution adds an extra carriage return character on return.

Post by Stephane CHAZELA » Sat, 15 Feb 2003 04:44:13



> I have made a test script that shows the problem:

> x=`echo -n "1234"`
> y="1234"

> echo -n $x > file1
> echo -n $y > file2

> After execution, file1 contains 5 characters; "1234"  AND a CR (Hex 0A).
> file2 don't.

That's not possible unless your bash is bogus.

0x0A is LF aka NL aka \n aka ^J, not CR.

command subsitution removes trailing LFs, and not quoting
$x with defaut value of IFS also removes trailing LFs.

Which version of bash (bash --version).

What does
printf %s "`printf %s 1234`" | od -tx1
printf %s `printf %s 1234` | od -tx1
x=`printf %s 1234`
printf %s $x | od -tx1
return?

--
Stphane

 
 
 

Problem: Command substitution adds an extra carriage return character on return.

Post by Bo Jacobse » Sat, 15 Feb 2003 04:56:07


I found the problem. A CR was embedded in the script-file just after
the last `. How it got there is a mystery, but now the script works at last.

Very ugly.

Bo

 
 
 

Problem: Command substitution adds an extra carriage return character on return.

Post by laura fairhe » Sat, 15 Feb 2003 06:11:34



>I have made a test script that shows the problem:

>x=`echo -n "1234"`
>y="1234"

>echo -n $x > file1
>echo -n $y > file2

>After execution, file1 contains 5 characters; "1234"  AND a CR (Hex 0A).
>file2 don't.

Ascii 0x0A is a *line-feed* character, LF.

The carriage-return CR is 0x0D

Quote:

>How do I avoid the extra CR when doing command substitution ?.

There is none. Trailing LF characters are stripped from the result
of a command substitution.

To be sure that you are not witnessing a bug (*very* unlikely ),
I have tested under Linux/bash (albeit an earlier version SuSE7.1)
and got the expected results, ie; no trailing LF character.

Please double check that you are not making a mistake somewhere,
try the following program;

#!/usr/bin/bash
x=`echo -n "1234"`
y="1234"

echo -n $x > file1
echo -n $y > file2

Then get the contents of the files via 'od'

od -txCa file2
od -txCa file1

output should be the same in both cases;

0000000 31 32 33 34
          1   2   3   4
0000004

Quote:>PS: I'm using the default shell (bash) in SuSE 8.1

It would be impossible anyway in your code given
even if the command substitution was bugged say and left
in the LF because you have the variables unquoted in
the 'echo' statements so the NEWLINEs (as SPACEs and TABs
would be ) will just be treated as argument deliminators
by the shell;

! $ x='1234

Quote:> '

! $ echo -n $x |od -txCa
0000000 31 32 33 34
          1   2   3   4
0000004
! $ echo -n "$x" | od -txCa
0000000 31 32 33 34 0a
          1   2   3   4  nl
0000005
! $

Quote:>Bo

seeyafrom
laura

--
alt.fan.madonna |news, interviews, discussion, writings
                |chat, exchange merchandise, meet fans....
                |Get into the groove baby you've got to... check us out!