10

I am new to Bash Script. I run the following simple script in Ubuntu like this:

sudo bash -x Script.sh

Output:

+ $'\r'
: command not found
+ $'\r'
: command not found
+ $'\r'
: command not found
+ adminEmail=$'[email protected]\r'
+ $'\r'
: command not found
' echo 'database name:
database name:
+ read $'dbname\r'

The actual script:

#!/bin/bash

# Installation script for latest Wordpress website on Ubuntu
# 
# Kave
# December 27, 2011


adminEmail="[email protected]"

echo "database name:"
read dbname

What are all these '\r' error messages coming up? Even the comments seem not to be understood...

7
  • It looks like you have a problem with the line endings, but I don't know how to fix this... Commented Dec 27, 2011 at 18:46
  • It works for me... Arch Linux i686 Commented Dec 27, 2011 at 18:46
  • 1
    From the command line do dos2unix scriptname scriptname. \r are carriage returns, did you create the file on a windows machine and sent it to linux? Commented Dec 27, 2011 at 18:49
  • yes, Jaypal, thats what I did. Commented Dec 27, 2011 at 18:54
  • 1
    Just do dos2unix scriptname and execute your script. Commented Dec 27, 2011 at 18:55

4 Answers 4

12

Simply do

dos2unix scriptname

and execute your script.

If you don't have the tool you can do the following to install it.

sudo apt-get install dos2unix

OR

Do the following command line hacks to convert the file.

tr -d '\015' < scriptname > scriptname.new

OR

sed -i 's/\x0D$//' scriptname
Sign up to request clarification or add additional context in comments.

Comments

9

You downloaded the file via a Windows machine, and you have CRLF line endings (that's \r\n in C and related languages).

Remove the DOS line endings. For example, edit the file in vim and change the format with :set fileformat=unix (plus Return) and then write the file back out. Alternative techniques could use the tr command, or dos2unix or dtou, depending on what's available.

Comments

1

Sounds like there are some extraneous non-printable characters in the file.
Open the file in Vi/Vim:
press [ESC] to get into command mode
then type :set list

1 Comment

And once you've got the characters listed, how would you go about removing them? Welcome to StackOverflow. Your answer is very much on the right lines, but it isn't yet complete.
1

Cut and paste your code from this page into an editor on your Ubuntu, so you'll get your line-endings fixed (\r\n on windows vs \n on Linux for instance)

If you save it as a new file I'm sure it'll work fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.