If you’ve been dabbling with shell scripting, you might have come across a strange and mysterious message: “bad for loop variable”. What does it mean? Is your computer mad at you? Did your loop break up with you? Relax! Let’s unravel this shell script surprise together.
What is a for loop anyway?
Before we dive deep into the error, let’s take a quick look at what a for loop usually does in a shell script.
for var in one two three
do
echo $var
done
This little script above will print:
- one
- two
- three
Great! The for loop takes each word and assigns it to var in each iteration.
Now, what if you wrote something that looks like this?
for ((i=0; i<5; i++))
do
echo $i
done
This is another kind of for loop, using C-style syntax. It looks a little different, right? Wave hello to the brackets!
This version is only available in bash, not in all shell types.
The moment the error appears…
You decide to be creative. You write a script in a file. Let’s say it looks like this:
#!/bin/sh
for ((i=0; i<5; i++))
do
echo $i
done
You proudly run it:
sh yourscript.sh
Suddenly, BAM! The shell yells:
yourscript.sh: 2: Syntax error: Bad for loop variable
Wait, what? It looks fine, right?
Welcome to the world of shell differences, where not all shells are created equal.
The cause of the chaos: Shell types
The default shell sh is not the same as bash. They might both sound friendly, but they have differences under the hood!
/bin/shis usually linked to a lightweight shell like dash (especially on Debian/Ubuntu)./bin/bashis the powerful one — with smart tricks like C-style loops.
If you run your script with sh, you’re probably using dash. And guess what — dash does not understand the ((...)) expression in for-loops.
So what exactly is a “bad for loop variable”?
This message usually appears when the shell can’t recognize the style of the loop or the syntax you’re using. Since ((...)) is special syntax that dash doesn’t get, it throws a tantrum and gives you the error message.
But don’t be too mad at it. It’s just confused. Like someone who doesn’t speak your language.
How to fix it
Now you have a few cool ways to fix this script and dodge that error:
1. Use the right shell
Instead of starting your script with:
#!/bin/sh
Use:
#!/bin/bash
This tells the system: “Hey, use Bash for this!” Bash understands the ((...)) loop syntax, so no more errors.
2. Stick to POSIX syntax
If you’re writing something that needs to run everywhere — even on minimal systems — maybe don’t use fancy Bash-only tricks.
So instead of C-style loops, do this:
i=0
while [ $i -lt 5 ]
do
echo $i
i=$((i+1))
done
This version works fine with sh, dash, and other shells that understand POSIX shell standards.
3. Run it with Bash directly
If your script still starts with #!/bin/sh, but you want to test it anyway, you can run:
bash yourscript.sh
or even:
/bin/bash yourscript.sh
This skips the whole sh thing and runs your script directly with Bash.
Check your shell: Are you really using Bash?
You can check what shell you’re in by typing this in your terminal:
echo $0
If it says bash, then you’re good.
If it says sh, dash, or something else — watch out!
Quick tips to remember
- If your script uses
((...))— make sure you’re in Bash. - Use
#!/bin/bashfor Bash scripts. Use#!/bin/shfor portable scripts. - When in doubt, test with
bash script.sh.
Bonus: What if you see this error and you’re not using C-style loops?
Sometimes, the error still shows up even if you think you’re doing things right. It could happen if you do something weird like this:
for 1 in 2 3 4
do
echo $1
done
The variable name must be a valid shell variable. You can’t use a number like 1 as a variable. Shells hate that.
Always name your loop variables with actual names:
for item in 2 3 4
do
echo $item
done
This keeps your shell smiling.
And… Why does this even matter?
If you’re writing scripts to automate your work, this error could trip you up at the worst time. Imagine running a script automatically on a server and it fails with a message like “bad for loop variable“.
You’d be scratching your head, wondering why your perfect loop turned into a loop of frustration.
Shells are like dialects
Think of it this way:
- sh is like classic, old-school English.
- bash is like modern English mixed with slang and emojis.
They both communicate, but one knows more hip phrases!
Wrapping up our loop of knowledge
That “bad for loop variable” error? It’s not scary. It’s just your shell saying, “Hey, I don’t get this syntax.”
Now you know how to:
- Recognize the mistake
- Switch to the right shell
- Write more compatible loops
So next time your script throws a tantrum, you’re ready. Be the shell whisperer you were born to be!