5

When a run a for statement in debian bash command line, it works fine. But when I run it in a sh script or run it with bash command, it's keeping report "error near unexpected token `do'" Where is the difference?

[leon@www] ~/tmp $ for i in {1..10}; do echo $i; done
1
2
3
4
5
6
7
8
9
10
[leon@www] ~/tmp $ bash for i in {1..10}; do echo $i; done
-bash: syntax error near unexpected token `do'

BTW, all works fine in centos enviorment.

3 Answers 3

6

Use the -c option so that bash reads the commands from the string you pass in. Also, use single quotes around the command.

bash -c 'for i in {1..10}; do echo $i; done'
Sign up to request clarification or add additional context in comments.

Comments

4

your bash command line ends with the first ;

so it gets executed separately as:

bash for i in {1..10};
do echo $i;
done

and man bash says command argument should be a file to load: bash [options] [file]

Comments

0

You can wrap all your script inside inverted commas or in a file. Because here, you're doing bash for i in {1..10} then do echo $i and so on. You should use -c option if you don't put it in a file.

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.