Editing / Converting LINUX Scripts from a Windows System


If you are a Windows Administrator that performs support for LINUX systems or a LINUX administrator that performs work on Windows systems, you may have stumbled into the dreaded /bin/bash^M: bad interpreter: no such file or directory.

The default text editor on a Windows system is Notepad. While a fine editor for Windows text files, it actually removes hair from cross-platform administrators who use it. Notepad would not be considered a modern multi-platform text editor. It can change a file format without your knowledge and confound you for an hour or two.

There are two primary differences between Windows and LINUX file formats.

The first difference is how lines are ended. LINUX systems end a line in a text file with a Line Feed (LF) character. Windows systems end their lines in text files with a Carriage Return and Line Feed (CRLF) (also known as EOL or End Of Line).

Carriage Return (CR): Line Feed (LF):
ASCII / Unicode                =             13 ASCII / Unicode                =             10
Hexadecimal                      =             0x0D Hexadecimal                      =             0x0A
PowerShell                        =             \r or `r PowerShell                         =             \n or `n


The second difference is encoding type. File editors in Windows may save files with different default file encoding types. For example, Notepad saves the file in Windows/ ANSI codepage format (Unicode). Unicode files contain a BOM or Byte Order Mark. The BOM is an additional Unicode character that is placed at the beginning of a file to signal the Unicode scheme.

LINUX file editors like VIM save with a default of UTF-8 w/o BOM or without the  Byte Order Mark. Shell interpreters that do not expect the BOM character will throw an error.  As a Windows administrator performing cross platform work, be careful where you edit files.

The best recommendation is to edit LINUX shell scripts and files within a LINUX editor. Since you are reading this… you have likely edited the files on another platform.  :-)

There are several articles, blogs and technical help sites that attempt to assist you. However, most articles address the LF / CRLF problem; not the file encoding problem.

On the UNIX / LINUX side, you can load packages like dos2unix to change the format and encoding of to a compatible version. This program allows you to convert files that you transferred to the LINUX platform.

On the Windows platform, there are multiple options as well. For those graphical users, Notepad++ (https://notepad-plus-plus.org/ ) is a powerful Free Open Source Software (FOSS) modern editor that supports conversion to UNIX format and UTF-8 w/o BOM encoding.

As a cross platform administrator, transferring files between platforms, editing and then transferring them back isn’t efficient. If you are attempting automation, a graphical editor may not work. PowerShell can easily meet your requirements.

Create a new script in PowerShell.  My script is called psdos2unix.ps1 (to give credit to those original creators on the UNIX side that got the name first).

param (



if ((Test-Path -Path $InputFile) -eq $true) {

     $CarriageReturnLineFeed = "`r`n?"

     $LineFeed = "`n"

     # Read in the content of your file in a raw single string and replace the CarriageReturn+LineFeed with just the LineFeed

     $FileInMemory = (Get-Content -Path $InputFile -Raw) -replace $CarriageReturnLineFeed,$LineFeed

     # Write out the file you just edited in memory back out but also specify the format as Encoding type UTF-8 no BOM.

     # https://msdn.microsoft.com/en-us/library/s064f8w2(v=vs.110).aspx

     $UTF8NoBomEncoding = New-Object System.Text.UTF8Encoding($False)

     [System.IO.File]::WriteAllLines($InputFile, $FileInMemory, $UTF8NoBomEncoding)

     Write-Host "Conversion complete."


     Write-Warning "The filename or path to the file name specified is incorrect."



Now you can easily convert your LINUX files via PowerShell by running:    .\psdos2unix.ps1 -InputFile <input.file>

PowerShell should not be confused as a hair restoration product. It just leads to you pulling less of yours out.