Thursday, September 6, 2012

Line Returns

The Setup
If you are running Linux as a guest OS within Windows using VirtualBox or VMware.
And if you have a Windows folder shared with Linux.
And if those folders have some shell scripts which you edit in Windows but execute in Linux.

Then there is a very high probability that you might get this error while running those script files on linux that were created in Windows:
$'clear\r': command not found 

I got this error for a simple hello world script:
# My first script
echo "Hello World!"

Windows represents End of Line (EOL) with two characters: CR + LF but Linux represents  End of Line (EOL)  with only LF (details)

So if you want to continue editing the source of these script file on Windows but run them on Linux, you will need to convert the EOL representations from Windows format to Linux format before you run the scripts on Linux.
If you are using Notepad++ as your source editor, you can achieve this by selecting the following option: Edit > EOL Conversion > UNIX Format

PS. This is important, otherwise your script files will be impotent! hah!

Carriage Return = CR = ASCII code 13 (in decimal) - '\r'
Line Feed = LF = ASCII code 10 (in decimal) - '\n'
These are vestiges of the typewriter era. There were two separate mechanical actions involved in beginning typing on a new line:

History: CR/LF goes back to the days of early printers where the codes
replicated the actions of a typewriter. CR (carriage return) returned
the carriage to the start of a new line. LF (line feed) advanced to the
next line. The lever at the left end of a typewriter combined moving
the carriage and advancing the line. Typists used to apply a CR/LF at
the end of every line and an extra CR/LF between paragraphs. As electric
typewriters came into use they would automatically apply the CR/LF when
the end of the line was reached. (link)

If you are editing Linux script files (bash) on Windows and are using GitHub for Windows to commit those files in one of your repositories, make sure to change the default line ending handling setting in your .gitattributes from:

# Auto detect text files and perform LF normalization
* text=auto


# Auto detect text files and perform LF normalization
* text eol=lf

You can then use Notepad++ to edit these script files on Windows and still use them properly on your Linux virtual machines (via shared folders on VirtualBox or VMware) without GitHub messing up the line conversion during commit and clone operations. Please note Windows' built-in notepad will not display these script files properly because it expects CR + LF as line endings and not just LF.

If in case you edited a script file on Windows using notepad and want to now execute on Linux, you can use the dos2unix utility to replace the CR-LF in a file with just LFs. After copying the script file to linux, execute the command:


and all line endings in your script file will be converted to linux format. Remember to "chmod +x" before you try to execute it.
You can get the dos2unix utility on Ubuntu (sudo apt-get install dos2unix) as well as on Arch Linux (pacman -S dos2unix).


Post a Comment