0

When i wrote a code for Shell Script in windows platform

#!/bin/bash
a=20
b=10
sum=`expr $a + $b`
echo $sum

but tried to execute it on UNIX platform it gave me error '20\r': command not found because Windows file ends with /r/n while UNIX file ends with /n.

Any Idea how to remove and process such error ?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Ankur Anand
  • 3,873
  • 2
  • 23
  • 44

1 Answers1

3

Use dos2unix - "[a] program that converts plain text files in DOS/MAC format to UNIX format" - or a similar command.

Repositories like Git or Hg they have support for 'line ending normalization'. Using a version control system is great for other reasons too!

If transferring via [S]FTP/SCP (eg. WinSCP) specify "TEXT mode" for the transfer the conversion will be done automatically - the newline transcoding is done as part of the copy/transfer process itself.

A number of "advanced" text editors (eg. [x]emacs, [g]vim, sublime, notepad++, and even Visual Studio) can also repair/normalize such files. These editors can also be used to write the file from the start :)

user2864740
  • 60,010
  • 15
  • 145
  • 220