-2
#!/bin/sh
for repo in repoA, repoB, repoC;
do
    echo Cloning $repo.
done

When I execute this (sh myscript.sh) I get the following:

myscript.sh: line 2: syntax error near unexpected token `$'\r''
'yscript.sh: line 2: `for repo in repoA repoB repoC;

Ideas?

4
  • 6
    Check for Windows-style CR/LF in the file. you could do, echo myscript.sh | od -c to peak at the characters. The commas are a problem, too. See cyberciti.biz/faq/bash-for-loop. Commented Jul 14, 2014 at 17:02
  • 1
    Get rid of the commas! Commented Jul 14, 2014 at 17:20
  • @IAmYourFaja - The second line of your output printed \r which moved the cursor back to where m was and printed ' thus the weird output. Try running dos2unix myscript.sh. This should convert CRLF to \n. file myscript.sh will tell you what kind of file is myscript.sh. cat -veT myscript.sh will print the contents of myscript.sh including non-printable characters. Commented Jul 14, 2014 at 17:44
  • possible duplicate of syntax error near unexpected token ' - bash Commented Oct 1, 2014 at 11:30

3 Answers 3

3

Windows uses two characters at the end of each line: '\r' and '\n'. Unix just uses '\n'. Presumably you're editing this in notepad and running it in cygwin, which is why you're getting this error.

Download the Notepad++ editor, which has an option for unix-style line endings under Edit / EOL Conversion.

Sign up to request clarification or add additional context in comments.

Comments

0

You're script was probably written on a windows machine and contains DOS newlines CR LF instead of Unix ones, LF

So you just need to convert the newlines.

You can do that using a variety of tools but my preference is dos2unix

To install it on CentOS, Fedora or RHEL do:

sudo yum install dos2unix 

To install dos2unix on Ubuntu or Debian:

sudo apt-get install tofrodos
sudo ln -s /usr/bin/fromdos /usr/bin/dos2unix

Now to actually do the conversion do:

dos2unix your_script.sh

Then run the script

sh your_script.sh

Comments

-4

You forgot a semicolon:

#!/bin/sh
for repo in repoA repoB repoC;
do
    echo Cloning $repo.
done

Note that repoA, repoB and repoC are being treated as variables in this context.

5 Comments

Thanks @d33tah (+1) - but still same error. Any ideas? I think it's interesting that the 2nd line of output has yscript instead of myscript...
@IAmYourFaja you've got some non-visible control characters in the file. If you copy/paste the one you typed in your question into a script, it will probably work.
@d33tah you don't need the semicolon. The line break takes care of it. And the repoA, repoB, and repoC are treated as literals in this context. The value of repo is set to each, in turn.
This is still not working; I copy/pasted the raw text from your answer into my script and got the same output as above in my question. I'm on Windows 7 using Cygwin, could that be the culprit?
@IAmYourFaja what editor are you using? I believe it's writing the file out in "DOS" format with the CR/LFs. Check for an option to use Linux/Unix style text.

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.