Simple Question

Friends,

I did following exercise

$ echo '' > test
$ od -b test

$ echo "">test
$ od -b test

$echo > test
$od -b test

Every time I got the following output
0000000 012
0000001

But 012 is octal value for new line character .
Even though there is no apperent new line character in test file
the output of command showed 012.
Will anybody please explain the reason for the output ?

What is the purpose of your input?

Also, what flavor of UNIX is this on?

I believe that a file begins with a newline char or some other unseen chars.

I had trouble in a script one time trying to test for 0 bytes because of bogus chars in the file even though it shows 0 bytes from an "ls filename" output.

Weird...

j1yant,

From the man page for echo, I found the following...

DESCRIPTION
The echo utility writes its arguments, separated by BLANKs
and terminated by a NEWLINE, to the standard output. If
there are no arguments, only the NEWLINE character will be
written.

So this explains why you always get a newline.

Also, in that man page, you'll see that some echos ( shell dependant ) allow for a flag to be passed to eliminate the adding of a NEWLINE to the end of the character.

Hope this helps...
T

Kelam...

Are you sure it's a NEWLINE character at the start of every file?

If you think about it a file with nothing in it only has a newline in it...

Because the file as "spaces followed by a newline". So your answer is yes. But the file has to be empty for this to be the case.

That is probably why my 0 byte test failed.

here is my output using your test criteria.

-root:/tmp> echo "" > test # no blank in between
-root:/tmp> od -b test
0000000 012
0000001

-root:/tmp> echo > test #echo nothing
-root:/tmp> od -b test
0000000 012
0000001

-root:/tmp> echo " " > test # echo one blank character
-root:/tmp> od -b test
0000000 040 012
0000002

Troccola,

Yes ,in the man page explantion is given as to why new line character appears.

I want to know which flag you passed to eliminate new line character. Will you please demonstrate it?

J1yant

With echo, you would want to do something like this:

echo "\c" > file

If that doesn't work, you might need to do this:

echo -e "\c" > file

But that best way to do it is:

>file