Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I got this error after execute shell script as below.

$ ./
./ line 3: syntax error near unexpected token `$'do\r''
'/ line 3: `for ((j=0; j<"$N"; j++)) ; do

Script below


# This script will launch 10 simulations for each value of the PARAM1_LIST.
# Simulations results will be stored in folders named "myTest-i-j", where i
# is the effective value of the PARAM1_LIST for each simulation and j
# is the 1 to 10 repetition for each i.

PARAM1_LIST=(1 2 5 10)

for (( j = 0 ; j < $N; j++ )); do
for i in ${PARAM1_LIST[@]}; do
java -jar N3Sim.jar myConfigFile.cfg myTest-${i}-${j} $i
share|improve this question
Hard to tell due to the formatting above, but it looks like you may need #!/bin/sh instead of !/bin/sh ? – Kent Jul 24 '13 at 8:26
The OP seems to have written a full shebang, but markup ate the hash. – Hennes Jul 24 '13 at 8:30
What error exactly? – Journeyman Geek Jul 24 '13 at 8:32
You can add a set -x below the shebang line to get more debug information. – Hennes Jul 24 '13 at 8:37
Also, I notice and error near $'do\r. That \r is a newline. Maybe that line got written or copied from a place with other line endings than which your interpreter expects. – Hennes Jul 24 '13 at 8:40
up vote 0 down vote accepted

Your script contains DOS newlines CR LF instead of Unix ones LF. $'\r' is a bash escape sequence for the CR control character which should not be normally contained in Unix text files. It was at the end of line 3 which is now the line 11 as you probably added the shebang sequence and comments later.

Solution is to convert the newlines:


Also please note that the script is using bash construct of array and the shebang should be #!/bin/bash.

share|improve this answer
Thanks for the answer. I just need to convert the script to unix format using cygwin. Everything I put in hash-tag just a simple explanation to what I'm trying to do. – leepyron Jul 25 '13 at 7:29
@leepyron I do not completely understand your comment. You can convert your script file using the dos2unix utility (as shown in the answer) which is part of Cygwin. If for any reason you cannot install the dos2unix package, you can use for example sed 's/\r//' > or tr -d '\r' < > See Alternatives to unix2dos conversion. – pabouk Jul 25 '13 at 15:10
@leepyron continuation: By "hash-tag" you mean the shebang sequence #!/bin/bash? It is a path to the script interpreter which will read and interpret your script file when you execute it. For example Korn Shell script would begin with #!/bin/ksh. – pabouk Jul 25 '13 at 15:13
@leepyron As I and Glenn wrote earlier: In your case change the #!/bin/sh at the file beginning to #!/bin/bash because your script is a Bash script. – pabouk Jul 25 '13 at 15:35
Hi @pabouk, I'm not sure what you're talking about. I'm still new with all this thing. My purpose only to create a script to automate simulations for a list of values of a parameter and repeat each simulation N times. I'm using an open source software with .cfg file. Right now, the script is working flawlessly after I convert it using dos2unix command in cygwin. Thanks for your attention. This is the guide that I follow: – leepyron Jul 26 '13 at 4:57

You are using bash features but using a sh interpreter. Change the shebang line to #!/bin/bash

To decomstrate:

$ /bin/sh
$ i=1
$ ((i++))
/bin/sh: i++: not found
share|improve this answer

Another possible solution using unix text editor vi:

open file in vi edit with vi command;

type in vi :set ff=unix command;

save file with :wq

It will save the file with unix line endings.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.