Bash For Syntax Error Bad For Loop Variable
Contents |
communities company blog Stack Exchange Inbox Reputation and Badges sign up log in tour help Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn
Syntax Error Bad For Loop Variable Shell
more about Stack Overflow the company Business Learn more about hiring developers or bash syntax error near unexpected token done' posting ads with us Ask Ubuntu Questions Tags Users Badges Unanswered Ask Question _ Ask Ubuntu is a question and answer bash syntax error near unexpected token else' site for Ubuntu users and developers. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise
Bash Syntax Error Near Unexpected Token Fi'
to the top Loop variable error in for loop up vote 1 down vote favorite 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
Bash Syntax Error Invalid Arithmetic Operator
this. scripts share|improve this question edited Jan 6 '14 at 11:58 Florian Diesch 46.7k12105131 asked Jan 6 '14 at 11:56 Gaurav Rai 814 add a comment| 2 Answers 2 active oldest votes up vote 3 down vote accepted 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. share|improve this answer answered Jan 6 '14 at 12:05 Florian Diesch 46.7k12105131 Is there any way to do it in sh mode? Thanks! –Ziyaddin Sadigov Feb 26 '14 at 18:48 1 i=0; while [ $i -le 5 ]; do echo $i; i=$((i+1)); done –Florian Diesch Feb 26 '14 at 19:32 add a comment| up vote 0 down vote 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 share|improve this answer edited Jan 17 '14 at 14:29 Florian Diesch 46.7k12105131 answered Jan 16 '14 at 11:55 smn_onrocks 27918 add a commen
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack bash syntax error near unexpected token then' Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack
Bash Syntax Error Near Unexpected Token Do'
Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Syntax error: Bad bash syntax error near unexpected token newline' for loop variable up vote 3 down vote favorite 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 http://askubuntu.com/questions/400936/loop-variable-error-in-for-loop But i have problem: alarmclock-vol.sh: 3: alarmclock-vol.sh: Syntax error: Bad for loop variable bash sh share|improve this question asked May 20 '15 at 18:51 Kwiatkowski 4217 6 Because sh isn't bash. for (( … )) is not available in sh. –kojiro May 20 '15 at 18:53 @kojiro: sh may or may not be bash; on some systems, /bin/sh is a symlink to /bin/bash, and the above script may work. In any case, you certainly shouldn't assume that it is. –Keith Thompson May 20 http://stackoverflow.com/questions/30358065/syntax-error-bad-for-loop-variable '15 at 19:14 1 @KeithThompson, though even if sh is a symlink to bash, bash behaves differently when invoked as sh (posix mode enabled). Therefore, even when sh is bash, "sh is not bash" still applies. –geirha May 20 '15 at 19:40 @geirha: On my Debian 6 system, /bin/sh is a symlink to /bin/bash, and the for (( ... )) syntax works in a script with #!/bin/sh; with #!/bin/dash it gives me "Syntax error: Bad for loop variable". (It's bash 4.1.5 if that matters.) –Keith Thompson May 20 '15 at 19:47 @KeithThompson, yes, some syntax still works (like for ((...)) in this case), some acts a little differently (the source and . builtins), while some syntax is disabled outright (like process substitution <(...) and >(...) ). Those are just examples that came to mind right now. –geirha May 20 '15 at 19:52 add a comment| 1 Answer 1 active oldest votes up vote 6 down vote accepted 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 share|improve this answer answered May 20 '15 at 18:54 geirha 2,6121021 work, thank you –Kwiatkowski May 20 '15 at 18:58 FYI, to change to bash, change your hashbang (the first line of your script) to #!/bin/bash &
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this http://stackoverflow.com/questions/22863783/bash-script-cant-get-for-loop-working site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up bash syntax error script - can't get for loop working up vote 3 down vote favorite Background Info: I'm trying to follow the example posted here: http://www.cyberciti.biz/faq/bash-for-loop/ I would like loop 9 times using a control variable called "i". Problem Description My code looks like this: for i in {0..8..1} do echo "i is $i" tmpdate=$(date -d "$i days" "+%b %d") echo $tmpdate done When I run bash syntax error this code, the debug prints show me: "i is {0..8..1}" instead of being a value between 0 and 8. What I've Checked So Far: I've tried to check my version of bash to make sure it supports this type of syntax. I'm running version 4,2,25(1) I also tried using C like syntax where you do for (i=0;i<=8;i++) but that doesn't work either. Any suggestions would be appreciated. Thanks. EDIT 1 I've also tried the following code: for i in {0..8}; do echo "i is $i" tmpdate=$(date -d "$i days" "+%b %d") echo $tmpdate done And... for i in {0..8} do echo "i is $i" tmpdate=$(date -d "$i days" "+%b %d") echo $tmpdate done They all fail with the same results. I also tried: #!/bin/bash for ((i=0;i<9;i++)); do echo "i is $i" tmpdate=$(date -d "$i days" "+%b %d") echo $tmpdate done And that gives me the error: test.sh: 4: test.sh: Syntax error: Bad for loop variable FYI. I'm running on ubuntu 12 EDIT 2 Ok... so i think Weberick tipped me off to the issue... To execute the script, I was running "sh test.sh" when in the code I had defin