syntax of for loop in linux shell scripting

33,647

Solution 1

You probably run it with sh, not bash. Try bash test1.sh, or ./test1.sh if it's executable, but not sh test1.sh.

Solution 2

A standard POSIX shell only accepts the syntax for varname in list

The C-like for-loop syntax for (( expr1; expr2; expr3 )) is a bashism.

You can get similar behavior in the standard POSIX shell using for c in $(seq 1 5)

Solution 3

What does

ls -l /bin/sh

give on your machine ?

Make sh a symbolic link to bash and then you can do sh ./test1.sh

Solution 4

Your shell script (as shown) runs in both Korn shell and Bash. Some thoughts:

  • You might need a space after the shebang (#! /bin/bash and not #!/bin/bash). However, Dennis Ritchie had originally specified the space is optional. Besides, it isn't the error you get with Bourne shell (you get syntax error: '(' unexpected instead).
  • Are you on a Windows system? Just a stab in the dark. This doesn't look like a Windows error.
  • Is this Solaris or HP/UX system? They might not be running true versions of Bash, or maybe an older version. However, even the oldest version of Bash recognizes the for ((x;y;z)) construct.

Try this:

#! /bin/bash
set -vx
echo "Random = $RANDOM"   #Test for bash/Kornshell. Will be blank in other shells
echo \$BASH_VERSINFO[0] = ${BASH_VERSINFO[0]} #Should only work in BASH
echo \$BASH_VERSINFO[1] = ${BASH_VERSINFO[1]}
echo \$BASH_VERSINFO[2] = ${BASH_VERSINFO[2]}
echo \$BASH_VERSINFO[3] = ${BASH_VERSINFO[3]}
echo \$BASH_VERSINFO[4] = ${BASH_VERSINFO[4]}
echo \$BASH_VERSINFO[5] = ${BASH_VERSINFO[5]}
for ((c=0, c<=5, c++))
do
    echo "Welcome $c times"
done
  • The set -xv will display all lines as they are executed.
  • The $RANDOM should display a value if this is either BASH or Kornshell (your for loop will work in either one).
  • The {$BASH_VERINFO[x]} should only be set if this is truly BASH. These aren't even set even if you run Korn shell after you're in BASH (unlike $SHELL which will still contain bash).

If the for loop still gives you trouble, just delete it. Somewhere in this script, we'll find out if you're really executing a bash shell or not.

Share:
33,647

Related videos on Youtube

mkab
Author by

mkab

Updated on October 19, 2020

Comments

  • mkab
    mkab over 3 years

    I have a problem implementing a for loop. I get this error when I execute my script

    test1.sh: 2: Syntax error: Bad for loop variable

    I don't understand this error.

    This is my script

    #!/bin/bash
    for (( c=1; c<=5; c++ ))
    do
    echo "Welcome $c times..."
    done
    

    can any one tell me syntax for for loop in sh(in ubuntu it links to dash shell) shell in ubuntu?

    • Robin Green
      Robin Green about 13 years
      I don't get the same error as you for that - I get unexpected end of file. Anyway, you have a missing semicolon.
    • karlphillip
      karlphillip about 13 years
      That works for me. You probably have a weird character hidden in the text.
    • Costi Ciudatu
      Costi Ciudatu about 13 years
      For bash the syntax seems fine. Can you try to make sure you're running the right shell ? ls -l /bin/bash, maybe ?
    • Amir Afghani
      Amir Afghani about 13 years
      This works for me when you put the done statement on its own line.
    • mkab
      mkab about 13 years
      I ran it using sh instead of bash. Works fine in bash. Thanks everyone.
  • Jim Garrison
    Jim Garrison about 13 years
    Works fine in bash as currently displayed in the post
  • mkab
    mkab about 13 years
    Krelin: You are right. I ran it using sh instead of bash or "./". It works well now. Thanks
  • Ankur Agarwal
    Ankur Agarwal about 13 years
    Yes works fine in bash. Make sh point to bash ( symbolic link) !