25

I get the below error while trying to execute a shell script,

$'\r': command not found: line 2:

Please suggest a solution for the same.

Below are the intial lines used in the script,

#!/bin/sh

if [[ $# -lt 1 ]]; then
   echo "ERROR Environment argument missing <dev,test,qa,prod>"
   export RC=50
   exit $RC
fi
Mohan
  • 251
  • 1
  • 3
  • 4
  • Please post the entire script or enough of it to allow people to reproduce the problem – Tony Mar 08 '11 at 09:52
  • \r\n is a windows end of line.. did u write it in windows? if so, just retype it up :P – Sigtran Mar 08 '11 at 09:52
  • #!/bin/sh if [[ $# -lt 1 ]]; then echo "ERROR Environment argument missing" export RC=50 exit $RC fi – Mohan Mar 08 '11 at 09:53
  • 8
    whoa, don't retype it: `dos2unix` it – glenn jackman Mar 08 '11 at 12:12
  • 1
    @Mohan, ...btw, `[[ ]]` is only guaranteed to be valid with `#!/bin/ksh`, `#!/bin/bash`, or another extended interpreter; with `#!/bin/sh`, only `[ ]` or `test` are guaranteed to work. – Charles Duffy Mar 14 '17 at 15:15
  • 2
    Possible duplicate of [Are shell scripts sensitive to encoding and line endings?](http://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) – tripleee Mar 16 '17 at 06:51
  • Don't `export` a variable just before you exit the script, that's completely pointless. – tripleee Mar 16 '17 at 06:57

7 Answers7

39

Your problem is that the file has Windows line endings. This can be caused by editing a file in Windows and trying to run it on a non-Windows system.

You can fix this problem using dos2unix to convert the line endings:

dos2unix ConstruedTermsXMLGenerator.sh

The corresponding utility to convert in the other direction is unix2dos.

Some systems have fromdos and todos.

Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
  • @Mohan, if you ftp your script from windows to *nix, make sure you transfer it as ascii. If you scp it, you have to use `dos2unix` on the remote side. Or use a text editor that can save in "unix" format. – glenn jackman Mar 08 '11 at 12:13
  • Notepad++ on Windows using Ansi codification does this to sh files. Use "Save as unix bash file" instead "Save". – Broken_Window Mar 15 '17 at 17:39
  • This is the perfect answer and should have been picked as the final answer. – Ahmad Nov 08 '22 at 21:30
21

You can use sed -i 's/\r$//' scriptname.sh

Replace the scriptname with actual script name.

biswaranjan
  • 211
  • 2
  • 3
16

I used notepad++ to convert the line endings.

Edit > EOL Conversion > UNIX/OSX Format

August Lin
  • 161
  • 1
  • 2
3

I had the same error and what I did was to transform the characters '\r' to '\n'. using this line:

tr '\r' '\n' < oldfile.sh > newfile.sh
mv newfile.sh oldfile.sh
chmod +x oldfile.sh
./oldfile.sh

I think you could also delete the '\r' characters by using:

tr -d '\r' < oldfile.sh > newfile.sh

tr is the command trasnform, and the -d is delete the following character.

I think the shell actually doesn't like '\r' character.

1

I had this exact issue when creating a .sh file on a Mac (unix) and executing it in Linux. Turns out that I had to set FileZilla FTP settings to 'Binary' transfer type:

  • "Settings>Transfers>File Types>Default transfer type" to "Binary" (instead of "Auto")
1

I got a different error message when running your script under /bin/sh, but when I switched to /bin/bash, it worked fine:

$ cat foo.sh
#!/bin/sh
if [[ $# -lt 1 ]];
    then echo "ERROR Environment argument missing"
    RC=50
    exit $RC
fi
$ sh foo.sh
foo.sh: 6: [[: not found
$ bash foo.sh
ERROR Environment argument missing

You've built in a bashism. This may or may not be a big deal for your organization. If you want to keep using bash-specific features, change the shebang line to #!/bin/bash and see if that helps.

tripleee
  • 175,061
  • 34
  • 275
  • 318
sarnold
  • 102,305
  • 22
  • 181
  • 238
  • Thanks sarnold for the response. I tried the same script after changing to bash and with debug turned ON. I still get the error, Below is the output, $ bash -xv foo.sh #!/bin/bash + $'\r' : command not foundnerator.sh: line 2: if [[ $# -lt 1 ]]; then echo "ERROR Environment argument missing " export RC=50 exit $RC fi ConstruedTermsXMLGenerator.sh: line 7: syntax error near unexpected token `fi' ConstruedTermsXMLGenerator.sh: line 7: `fi' – Mohan Mar 08 '11 at 10:56
  • @Mohan, then perhaps @Sigtran is correct; did this file originate on Windows? If so, run `dos2unix` or `dtox` or one of those similar sorts of tools to remove all the extra `\r` characters from the file. – sarnold Mar 08 '11 at 10:58
  • But, the same script works fine when it is placed in /tmp directory and executed. How is this possible? – Mohan Mar 08 '11 at 13:27
  • Running it with `bash -vx` is wrong if the shebang says `#!/bin/sh`, or vice versa. See also http://stackoverflow.com/questions/5725296/difference-between-sh-and-bash – tripleee Mar 16 '17 at 06:53
0

You can just replace '\r' with '' (nothing), this will do the trick. If you have any GUI editor (Notepad++) recommended directly find/replace.

If you are only doing it on Linux server then use something like:

sed -i 's/old-text/new-text/g' input.txt

But Since '\r' comes only if you have edited it in windows as it's a new line character (\r\n), so first method should work.

blackgreen
  • 34,072
  • 23
  • 111
  • 129
ISHAAN
  • 3
  • 4