vi editor for loop syntax problem
for (( i=0; i<=5; i++ ))
do
echo "$i Echoed"
done
this is what wrote in "bubble.sh" with vi editor and it gave me this error "bubble.sh: 1: bubble.sh: Syntax error: Bad for loop variable
"
i don't seem to understand why this is happening..please help
Question information
- Language:
- English Edit question
- Status:
- Answered
- Assignee:
- No assignee Edit question
- Last query:
- 2013-09-27
- Last reply:
- 2013-09-27
Ubfan (ubfan1) said : | #1 |
Your code is just fine and works FOR BASH. Look a t the beginning of your script, and if you have a #!/bin.sh, you are not even running bash. Change it to #!/bin/bash
Thomas Krüger (thkrueger) said : | #2 |
You have to add
#!/bin/bash
as the first line.
or you can run:
sh bubble.sh
The file extension means NOTHING in Linux. It's always confused me why people feel the need to add them. This isn't Windows. You can rename a .jpg to a .png and the OS will still see a jpeg format image.
The file is a list of instructions, but the shell has no idea what interpreter to use, so it complains. Adding:
#!/bin/bash
As people have said tells the system to use sh as the interpretter and it will run, you can code in python and use:
#!/usr/bin/python
In the same manner. Remember to also mark the file as executable.
andy@fileserver:~$ nano t; chmod +x ./t
andy@fileserver:~$ ./t
0 Echoed
1 Echoed
2 Echoed
3 Echoed
4 Echoed
5 Echoed
andy@fileserver:~$ cat ./t
#!/bin/bash
for (( i=0; i<=5; i++ ))
do
echo "$i Echoed"
done
Just so you know it works :)
Suman Mitra (sumanmitra97) said : | #5 |
#!/bin/bash
for (( i=0; i<=5; i++ ))
do
echo "$i Echoed"
done
i wrote this and this showed up....
"suman@
bubble.sh: 2: bubble.sh: Syntax error: Bad for loop variable"
still doesn't work... :(
Try:
chmod +x ~/bubble.sh; ~/bubble.sh
Does it then run?
What is the output of:
cat /etc/issue
Thanks
Suman Mitra (sumanmitra97) said : | #7 |
suman@Cyber-
0 Echoed
1 Echoed
2 Echoed
3 Echoed
4 Echoed
5 Echoed
suman@Cyber-
Ubuntu 12.04.3 LTS \n \l
it worked with the first command i don't get it...what's going on..??
If you mark the file as executable it will be treated as a script. You have the right header so now the 'sh' bit you were using to attempt to run it with is not needed. The '.sh' file extension does nothing at all for the file, its just a naming convention Windows users seem to hang on to as they seem to refuse to have files without file extensions. Weird really
Suman Mitra (sumanmitra97) said : | #9 |
Oh.I get it.But now when i removed the extension and wrote "sh bubble" it says this "bubble: 2: bubble: Syntax error: "(" unexpected".What do i need to do work around that?
Can you help with this problem?
Provide an answer of your own, or ask Suman Mitra for more information if necessary.