Ubuntu prompts syntax error: Bad For loop variable when performing arithmetic operations

Source: Internet
Author: User
Tags posix
Syntax error: Bad For loop variable solution

A simple shell script is written in Ubuntu, and the value of 1 + 2 + 3... + 100 is calculated using the for... do... done structure. An error is always returned.
Script:
#! /Bin/bash
# Information

Path =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Bin
Export path

Read-P "Please input a num" Num
Sum = 0
For (a = 0; A <= $ num; A = a + 1 ))
Do
Sum = $ ($ sum + $ ))
Done
Echo "the sum is => $ sum"
Exit 0

Error syntax error: Bad For Loop Variable

Solution: sudo dpkg-reconfigure dash
Select No from the selected items.

Starting from Ubuntu 6.10, UBUNTU replaces the default bash shell with the dash shell. It shows that the/bin/sh link is down with/bin/dash instead of the traditional/bin/bash.

Ubuntu edgy is the first version released with Dash as the default shell, which seems to be affected by Debian. There is an official explanation in the Wiki, https://wiki.ubuntu.com/dashasbinsh. the main reason is that dashis smaller, runs faster, and is compatible with posix.

However, the current problem is that the shell replacement causes many script errors. After all, many of the current scripts are not 100% POSIX compatible.

As mentioned in the Wiki, how can we change the default shell back to bash?

Run sudo dpkg-reconfigure dash on the terminal

Select No

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.