-1

I know that we can use the following syntax to create a for loop in linux or android shell script :

for i in 1 2 3 4 5
do
echo $i
done

It is working as expected when written directly in terminal : enter image description here

But when the code is saved in a .sh file and called in terminal using the 'sh' command, it shows - "syntax error 'do".

Code in a .sh file : enter image description here

File called in terminal : enter image description here

Can anyone explain what exactly is happening here ? I am using Terminal Emulator in android. When I am working with other commands in the .sh file, there is no problem with newline encoding.

Puspam
  • 2,137
  • 2
  • 12
  • 35

1 Answers1

-1

Oh yes, that's right. When I added another new line before the 'do' command, it worked.

Previously I wrote this :

for i in 1 2 3 4 5
do
    echo $i
done

Afterwards, I wrote this :

for i in 1 2 3 4 5

do
    echo $i
done

Note that there are two new lines before 'do'.

Puspam
  • 2,137
  • 2
  • 12
  • 35