Bash for syntax error bad for loop variable

I'm trying to write a script that will vol up radio in the background #!/bin/sh for (( i = 80 ; i <= 101; i++ )) do amixer cset numid=1 i$% sleep 60; done But i have problem: alarmcloc...

I’m trying to write a script that will vol up radio in the background

#!/bin/sh

for (( i = 80 ; i <= 101; i++ )) 
 do 
  amixer cset numid=1 i$% sleep 60;
done 

But i have problem:

alarmclock-vol.sh: 3: alarmclock-vol.sh: Syntax error: Bad for loop variable

asked May 20, 2015 at 18:51

Kwiatkowski's user avatar

5

The for (( expr ; expr ; expr )) syntax is not available in sh. Switch to bash or ksh93 if you want to use that syntax. Otherwise, the equivalent for sh is:

#!/bin/sh

i=80
while [ "$i" -le 101 ]; do
    amixer cset numid=1 "$i%"
    sleep 60
    i=$(( i + 1 ))
done 

answered May 20, 2015 at 18:54

geirha's user avatar

geirhageirha

5,6171 gold badge29 silver badges35 bronze badges

1

use bash instead of sh

bash alarmclock-vol

Sven Eberth's user avatar

Sven Eberth

3,02712 gold badges23 silver badges28 bronze badges

answered May 30, 2021 at 14:22

Rishab's user avatar

try using

#!/usr/bin/bash

instead of

#!/bin/bash

answered Sep 26, 2021 at 4:15

Muhammad Ahsan Mangi's user avatar

1

Another way you can achieve this is by giving the file execution permission. Below are the commands:

Instead of sh filename.sh

Do this:

chmod +x filename.sh
./filename.sh

You can see in this picture

for the same code that didn’t run with sh but runs after giving execution permission.

The shell script that I used here is:

#!/bin/bash
for ((i = 1; i <= 10 ; i++)); 
do
  echo $i
done

answered Jul 6, 2022 at 3:54

Aniruddha Ghosh's user avatar

When I’m using for like

 for i in 1 2 3 4 5 

then my file contains #!/bin/sh at the top.

But when I’m using

for(( i = 0; i<=5; i++))

then it is showing error

Syntax error: Bad for loop variable

and running properly when I remove shebang. Please tell me the reason behind this.

Florian Diesch's user avatar

asked Jan 6, 2014 at 11:56

Gaurav Rai's user avatar

for(( i = 0; i<=5; i++)) is Bash specific and doesn’t work with plain Bourne shell (/bin/sh).

If you remove the shebang the script is run by your current shell (which likely is Bash) so it works.

Replace #!/bin/sh with #!/bin/bash to make the shebang work.

answered Jan 6, 2014 at 12:05

Florian Diesch's user avatar

Florian DieschFlorian Diesch

84.6k17 gold badges223 silver badges214 bronze badges

3

I have solved this problem by using ./ instead of sh command.
For an example if you put sh test.sh instead just make your command as ./test.sh And most probably problem will be solved.

answered Nov 21, 2016 at 18:12

dilantha111's user avatar

for(( i = 0; i<=5; i++))

for this type of loop only runs on Bash shell. so, if you want to run this, then try this command :

$bash filename.sh

I think It will work fine. and see this one also.

Community's user avatar

answered Mar 27, 2017 at 11:15

Sudip Das's user avatar

Sudip DasSudip Das

3002 silver badges10 bronze badges

Try this out may be this could solve your problem

#!/bin/bash
j=0
for (( i=1; i <= 5; i++ ))
do
 echo "the loop is runing $i time: and value of j is $j"
 j=`expr $j + 1`
done

Florian Diesch's user avatar

answered Jan 16, 2014 at 11:55

smn_onrocks's user avatar

smn_onrockssmn_onrocks

5265 silver badges14 bronze badges

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash function using variable in it syntax error

The below bash function uses multiple variables CODING, SAMPLE, SURVEY, andvariant
in it. The user selects the cap function and details are displayed on the screen using the $SURVEY variable, the directory is changed to $SAMPLE and the samples.txt is opened so the user can select the sample to… (6 Replies)

Discussion started by: cmccabe

2. UNIX for Dummies Questions & Answers

Syntax error in for loop

I am using simple for loop, but getting syntax error when I run the code

code

#!/bin/ksh
pls enter number
read n
for(i=1; i<=n; i++)
do
echo $i
done

syntax error

+ pls enter number
+ read n (5 Replies)

Discussion started by: stew

3. Shell Programming and Scripting

Variable syntax error in $?

hi all ,

i just tried to take the status of previous command inside the script using
echo $?. It throws me a variable syntax error , but when i use echo $? as an individual command it works perfectly .

can anyone Please tell me why am getting a variable syntax error when i use echo $?… (7 Replies)

Discussion started by: Rahul619

4. UNIX for Dummies Questions & Answers

[Solved] Syntax error for awk in a loop

can some one please tell me what is the problem with my syntax:confused:

I have 100 files in one folder
1. want to read each of the line by line
2. calculate their number of the words between the first word and the last word of each line
3. create file for each file with number of words… (8 Replies)

Discussion started by: A-V

5. Shell Programming and Scripting

IF loop syntax error

I am trying to run a menu option though IF loops. I keep getting errors not allowed the menu to be processed correctly. Currently it will accept the first 2 statements but then crash on the 3rd. The 2nd and 3rd have the same syntax, so I do not understand why it breaks.

#!/bin/bash

while… (4 Replies)

Discussion started by: Ironguru

6. Shell Programming and Scripting

Syntax error: Bad for loop variable

I’m getting an error while running this script. Need help.

set -x verbose #echo on
clear #clear the screen

USERNAME=»bbb»
PASSWORD=»password»
SERVER=»192.168.1.100″
WAIT_TIME=300
FILE_PATH=»/home/users/xxx/MMM» # local directory to pickup *.dat file
REMOTE_PATH=»/Drop_off/xxx/yyy» #… (17 Replies)

Discussion started by: clgz2002

7. Shell Programming and Scripting

Bash (Ubuntu server): Syntax error: «|» unexpected in While-loop

Hello forum,

I hope my problem is easy to solve for someone in here!

My main task is to copy a large amount of imap-accounts from one server to another. There is a tool (Perl) called imapsync which does the job exellent. Unfortunately I’m only able to run it on one account at a time.

After… (3 Replies)

Discussion started by: primaxx

8. Shell Programming and Scripting

for loop not working — syntax error at line 6: `end of file’ unexpected

I have a file called test.dat which contains

a b

I have written a shell script called test.sh

for i in `cat test.dat`
do
echo $i
done

When i run this script using

sh test.sh

I get this message —

test.sh: syntax error at line 6: `end of file’ unexpected

What is the… (3 Replies)

Discussion started by: debojyoty

9. Shell Programming and Scripting

Syntax error on variable assignment

Hello all,

I have «inherited» a Korn shell script I’m supposed to maintain, and running a «sh -n» on it, I got this syntax error:

script.sh: syntax error at line 63: `OB_DEVICE=$’ unexpected

The line in cause is the first occurence of the usage of perl one-liners. The whole line:
(2 Replies)

Discussion started by: AdrianM

10. Shell Programming and Scripting

syntax error in while loop

Hi,

I have the following script (compile_mercury) and I get this error: I have no idea why…and I have written this script completely in linux (bash) and not in windows.

****************
./compile_mercury: line 136: syntax error near unexpected token `done’
./compile_mercury: line 136:… (1 Reply)

Discussion started by: habzone2007

  • Home
  • Forum
  • The Ubuntu Forum Community
  • Ubuntu Specialised Support
  • Development & Programming
  • Programming Talk
  • script.sh: 1: Syntax error: Bad for loop variable

  1. script.sh: 1: Syntax error: Bad for loop variable

    Code:

    for (( i = 0; i <= 4; i++ ))
    do
    	echo $i
    done


  2. Re: script.sh: 1: Syntax error: Bad for loop variable

    /bin/sh is a symlink to dash, not bash.is a bash-ism, not recognized by dash.

    So try either

    PHP Code:


    #!/bin/bash
    for (( 0<= 4i++ ))
    do
        echo 
    $i
    done 



    or

    PHP Code:


    #!/bin/shfor i in $(seq 0 4)
    do 
        echo 
    $i
    done 




  3. Re: script.sh: 1: Syntax error: Bad for loop variable

    Code:

    for i in {0..4}
    do
      echo $i
    done

  4. Re: script.sh: 1: Syntax error: Bad for loop variable

    PHP Code:


    #!/bin/bash

    for (( 0<= 4i++ ))

    do

        echo 
    $i

    done 



    PHP Code:


    script.sh2Syntax errorBad for loop variable 




  5. Re: script.sh: 1: Syntax error: Bad for loop variable

    Quote Originally Posted by ghostdog74
    View Post

    Code:

    for i in {0..4}
    do
      echo $i
    done

    Not trying to use «for i in» syntex ,but the «for (( ))» syntex.


  6. Re: script.sh: 1: Syntax error: Bad for loop variable

    Quote Originally Posted by colau
    View Post

    PHP Code:


    #!/bin/bash

    for (( 0<= 4i++ ))

    do

        echo 
    $i

    done 



    PHP Code:


    script.sh2Syntax errorBad for loop variable 



    After ./script.sh
    After sh script.sh

    PHP Code:


    script.sh2Syntax errorBad for loop variable 




  7. Re: script.sh: 1: Syntax error: Bad for loop variable

    Quote Originally Posted by unutbu
    View Post

    /bin/sh is a symlink to dash, not bash.

    is a bash-ism, not recognized by dash.

    So try either

    PHP Code:


    #!/bin/bash

    for (( 0<= 4i++ ))

    do

        echo 
    $i

    done 



    or

    PHP Code:


    #!/bin/sh
    for i in $(seq 0 4)

    do 

        echo 
    $i

    done 



    Thank you.


Bookmarks

Bookmarks


Posting Permissions

Понравилась статья? Поделить с друзьями:
  • Bash error dev null
  • Bash error codes
  • Bash echo write error invalid argument
  • Bash disable error output
  • Bash deb команда не найдена как исправить