0

For specific reasons, I'm trying to create a second file identical to the first one without using cp by using cat and echo

My current bash code is

contents=$(cat file1)
echo $contents > file2

and I have the diff here

https://www.diffchecker.com/UIbkWHi5

The differences have to do with spaces/newlines, and I've played around with some echo flags (namely -n and -e) to no avail. Where can I go from here?

user3613290
  • 461
  • 6
  • 18
  • 3
    `echo "$contents"` -- quotes matter. Or even better, `printf '%s\n' "$contents"` – Charles Duffy Mar 12 '20 at 17:38
  • 1
    To understand the differences that still persist after quoting is fixed (until you switch from `echo` to `printf`), see the excellent answer by Stephane on https://unix.stackexchange.com/questions/65803/why-is-printf-better-than-echo – Charles Duffy Mar 12 '20 at 17:39
  • that works, thank you! – user3613290 Mar 12 '20 at 17:41
  • (You can't get an exact byte-for-byte representation read through a command substitution unless you know that your file is a valid UNIX text file, meaning that every line *including the last one* ends with a linefeed, and that the file contains no NUL characters). – Charles Duffy Mar 12 '20 at 17:41

0 Answers0