-5
#!/bin/bash

Clear
for ((row=1;row<=5;row++))
do
     for((col=1;col<=row;col++))
     do
          echo -n "$row "
     done
     echo "  "
done

I use kali Linux. When I try to run this script then error show me.

Pra20: 3: pra20: Syntax error:Bad for loop variable

Piro
  • 1,367
  • 2
  • 19
  • 40
Sumit Sharma
  • 1
  • 1
  • 5

2 Answers2

1

The message comes from dash, which is used as sh on some Linux's. Therefore you are using dash, which does not support this syntax.

With the assumption that your script name is Pra20, you are probably running your script like this:

sh Pra20

instead of:

bash Pra20

or

./Pra20

Only the final method will read the #! line. Also ensure you have execute access with chmod u+x Pra20.

See also: syntax of for loop in linux shell scripting

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
cdarke
  • 42,728
  • 8
  • 80
  • 84
0

for loop syntax used in your code is not supported by shell(sh) that's why It's showing the error.

to solve this problem, either first make the script as executable and then run as

chmod  +x  kali.sh
./kali.sh 

Otherwise use below for loop syntax

for var_name in test
do
         body of for loop
done

above syntax is supported by sh shell so you can run your script as sh kali.sh

Achal
  • 11,821
  • 2
  • 15
  • 37