0

I have an issue where ^M characters are being inserted into a script file. I have a Bash script:

#!/bin/bash

# Do something
echo "hello world"

Using PowerCLI I copy the script file from Windows to a Ubuntu virtual machine:

Copy-VMGuestFile -Source "C:\test.sh" -Destination /tmp/test.sh -LocalToGuest -GuestUser root -GuestPassword p@ssword -VM VM001

After copying, when opening the file with vi the format shown is:

#!/bin/bash^M

^M
# Do something^M
echo "hello world"^M

Is there a way to stop ^M being added when copying the file? I can replace the characters using sed, but this is not a clean solution.

LightningWar
  • 915
  • 1
  • 19
  • 35
  • 2
    They are not added when copying, they are added on Windows. Windows is using carriage-return and linefeed at the end of a line. linux is only using one of them, linefeed i think, the ^M then is the carriage-return. – frank Aug 23 '18 at 07:25
  • I agree with @frank. The trap here is that you open the file on both sides with the same editor (in this case, vi) so if something is new it means it has been inserted in the meantime, right? But you can probably see them on the Windows side too if you run vi with the `-b` option (e.g. `vi -b "C:\test.sh"`) or by typing `:e ++ff=unix` in the vi window. – vdavid Aug 23 '18 at 08:00

1 Answers1

0

The control characters aren't being added by the copy, they are part of the Windows file.

On Windows lines are terminated with both a carriage return and line feed. On Unix it's just the line feed so you are seeing the superfluous carriage return character in the file.

Depending on your editor of choice on Windows you may be able to change the behaviour to be more Unix friendly. For example with sublime it's

View->Line Endings->Unix

Alternatively you can run the script through a translator before/after the copy. Which would do the same as you are doing with sed.

Gary M
  • 428
  • 5
  • 13