-1
$ echo testing > testfile
$ irb
2.5.1 :001 > IO.read('testfile')
=> "testing\n"

Trying to understand where the newline is coming from as it's clearly not in the file.

Chris B
  • 764
  • 1
  • 7
  • 19
  • 1
    "Trying to understand where the newline is coming from as it's clearly not in the file." – How have you determined that "it's clearly not in the file"? According to the commands you posted, there "clearly" *should* be a newline in the file, unless your `echo` is broken. – Jörg W Mittag Sep 22 '18 at 08:51
  • My test was opening it in vim, but that wasn't good enough. Apparently my echo is functioning as documented. #PEBKAC – Chris B Sep 23 '18 at 03:15

1 Answers1

7

But the newline is in the file, echo adds it. You can see for yourself with a hexdump:

$ echo testing > testfile
$ hexdump testfile 
0000000 74 65 73 74 69 6e 67 0a                        
0000008

That 0x0a is your newline.

And you can ask your shell (presumably bash) about echo:

$ help echo
echo: echo [-neE] [arg ...]
    Output the ARGs.  If -n is specified, the trailing newline is
    suppressed. [...]

So if you say echo -n testing > testfile, you'll get the results that you're expecting.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • Clean answer. It may be worth mentioning that text files should end with a newline if you want to conform to the standard. https://stackoverflow.com/questions/729692/why-should-text-files-end-with-a-newline – 3limin4t0r Sep 22 '18 at 12:39