Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

\r character in shell script

Tags:

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
like image 455
Mohan Avatar asked Mar 08 '11 09:03

Mohan


People also ask

What is \r in a script?

10. The \r issue suggests you have edited the script file on a Windows machine. Windows uses \r\n as the line terminator while Linux (and most other operating systems) use \n alone. When you edit a file in Windows, Windows will add \r to the end of the lines and that will break your scripts.

What \r does in Linux?

The character '\r' is carriage return. It returns the cursor to the start of the line. It is often used in Internet protocols conjunction with newline ( '\n' ) to mark the end of a line (most standards specifies it as "\r\n" , but some allows the wrong way around).

What is r in shell?

R-shell maintains a set of variables which can be defined and modified through a set of UNIX-like commands, and later accessed from independent user programs. R-shell commands generally fall into three groups: general commands, object commands and manipulator commands.

What is $() in bash script?

$() Command Substitution According to the official GNU Bash Reference manual: “Command substitution allows the output of a command to replace the command itself.


2 Answers

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.

like image 128
Dennis Williamson Avatar answered Oct 20 '22 07:10

Dennis Williamson


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

Replace the scriptname with actual script name.

like image 37
biswaranjan Avatar answered Oct 20 '22 07:10

biswaranjan