DEV Community

Cover image for How i spent 1 hour of my time for a line break in a Bash.

Posted on

How i spent 1 hour of my time for a line break in a Bash.

I hope this article will help you if you want to use Bash to create a small script.

So yes, I confirm, I spent 1 HOUR OF MY TIME for a little break.

'A panda rage'

Why this happening ?

Because line break in Windows are not the same for Linux and when you run your Bash script you can have an error like this :

'The problem'

And you can search for a long time in your codes, why this error appeared.

And this error appear just because the encoding of your file using the unicode of your line break is "CR+LF" (000D + 000A) instead of "LF" (000A).

CR is for "cariage return" and LF is for "line feed".

When you press enter, one of this Unicode is used. And Linux doesn't like the CR+LF because he represents all line endings by a LF and in my case, he gave me some problems.

How to solve it ?

To solve it, you have many solutions. The most easily it's to use your favorite IDE and change the line ending.

For exemple in VSCode it's at the bottom.
VSCode tips

If you use Git, you can use a file named .gitatttributes. You can in this file, specify to get what is the end of the line of a specific file.

*.sh        text eol=lf
*.md        text=auto
*.txt       text
Enter fullscreen mode Exit fullscreen mode

In the first line we declare the end of the lines for all files with .sh extension by LF.

And the last solution is to use the command dos2unix you can use to convert all lines endings of a file.

And you, have you ever have an absurd problem like that ?

Main sources :

The website solves my problems (and save me).

Microsoft explains line endings.

Top comments (0)