Tag Archives: (ubuntu)

How to Solve Syntax error: Bad for loop variable (ubuntu)

A simple shell script is written in Ubuntu, which uses for.. do.. done structure to calculate the value of 1 + 2 + 3… + 100, and the result always reports an error
script:
/ bin/bash
read -p “Please input a num ” num
sum=0
for ((a=0; a<=$ num; a=a+1))
do
sum=$(($sum + $a))
done
echo “the sum is ==> $ Sum ”
Exit 0

the error is syntax error: bad for loop variable
solution: sudo dpkg reconfiguration dash
select no from the options
from Ubuntu 6.10, Ubuntu will replace the previous default bash shell with dash shell; Instead of the traditional/bin/bash, the/bin/sh link reverses/bin/dash
Ubuntu edge is the first version to release dash as the default shell, which seems to be influenced by Debian. There’s an official explanation in the wiki, https://wiki.ubuntu.com/DashAsBinSh The main reason is that dash is smaller, faster and compatible with POSIX<However, the problem is that many scripts make mistakes due to the replacement of shell. After all, many scripts are not 100% POSIX compatible executing sudo dpkg reconfiguration dash on the terminal
and then selecting No