Bash For Loop Syntax Error Operand Expected
Contents |
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
Bash Syntax Error Operand Expected (error Token Is )
Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation syntax error operand expected (error token is = ) Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just
Bash (error Token Is ")
like you, helping each other. Join them; it only takes a minute: Sign up Bash 'for' loop syntax? up vote 10 down vote favorite 2 What is the syntax for a Bash for loop? I have tried: syntax error in expression error token is for (($i=0;$i<10;$i ++)) do echo $i done I get this error: line 1: ((: =0: syntax error: operand expected (error token is "=0") bash shell scripting share|improve this question edited Feb 13 at 18:52 Peter Mortensen 10.2k1369107 asked Jul 28 '11 at 4:32 user862489 4 First google search result for "bash for loop" is pretty good, but this page is much better. –Ray Toal Jul 28 '11 at 4:37 add a comment| 3 Answers expr: syntax error 3 active oldest votes up vote 13 down vote accepted Replace for (($i=0... with for ((i=0;i<10;i++)) share|improve this answer edited Aug 28 '13 at 21:08 answered Jul 28 '11 at 4:33 jman 7,81142150 Thanks! That was quick! –user862489 Jul 28 '11 at 4:35 @Laurent, reverted as it doesn't fit in with the rest of the answer (replace x by y). –jman Aug 28 '13 at 21:08 add a comment| up vote 11 down vote The portable way is: for i in `seq 0 9` do echo "the i is $i" done share|improve this answer edited Feb 13 at 18:52 Peter Mortensen 10.2k1369107 answered Jul 28 '11 at 21:50 Michał Šrajer 14.8k23054 1 That would loop from 1 to 10 instead of 0 to 9. –this.lau_ Aug 22 '13 at 14:45 1 @Laurent: Fixed that. Good catch. –Michał Šrajer Sep 9 '13 at 7:00 add a comment| up vote 5 down vote Another way for i in {0..9} do echo $i done share|improve this answer answered Jul 28 '11 at 4:37 Eric Fortis 8,31552542 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By po
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
Integer Expression Expected
Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation bash let Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like
Unary Operator Expected
you, helping each other. Join them; it only takes a minute: Sign up Syntax error: operand expected (error token is “+”) up vote 5 down vote favorite I'm writing a script in bash and I get this http://stackoverflow.com/questions/6854118/bash-for-loop-syntax error: ./P4.1: line 10: +: syntax error: operand expected (error token is "+") And this is my code: #!/bin/bash read string echo $string >| temp num1= cut -d" " -f1 temp num2= cut -d" " -f2 temp num3= cut -d" " -f3 temp while [ $num1 -gt $num3 ] do echo $num1 num1=$[$num1+$num2] done What's wrong and how do I fix it? Thanks. bash unix share|improve this question asked Nov 24 '13 at 16:03 shoham http://stackoverflow.com/questions/20176640/syntax-error-operand-expected-error-token-is 3372417 Why the downvote? –Blue Ice Nov 24 '13 at 16:27 Check also the -gt part, should't it be -lt ? –user3014562 Nov 24 '13 at 16:31 add a comment| 3 Answers 3 active oldest votes up vote 5 down vote accepted Combination of ceving and Tomek's: #!/bin/bash read num1 num2 num3 while [ $num1 -lt $num3 ] do echo $num1 num1=$((num1+num2)) done share|improve this answer answered Nov 24 '13 at 16:32 Blue Ice 4,36542039 Yes, the string seems to be the problem –user3014562 Nov 24 '13 at 16:35 I need to get the input as a whole string and then divide it up. Why do you use $(( )) instead of $[ ] like I was tought? –shoham Nov 24 '13 at 16:39 Well, either way is correct. You can do whichever one you choose. And this should read the numbers and it should divide them up. It reads the numbers all at once, delimited by spaces, and they are already separated when read. –Blue Ice Nov 24 '13 at 17:53 or: for ((; num1 < num3; num1 += num2)); do echo $num1; done –rici Nov 24 '13 at 21:29 add a comment| Did you find this question interesting? Try our newsletter Sign up for our newsletter and get our top
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 http://unix.stackexchange.com/questions/176902/bash-shell-how-to-do-nested-loop-without-syntax-error-operand-expected Business Learn more about hiring developers or posting ads with us Unix & Linux Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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 to the top bash syntax error shell - how to do nested loop without “syntax error: operand expected” [duplicate] up vote 0 down vote favorite This question already has an answer here: How can I use $var in a shell brace expansion of a sequence? 2 answers In bash, is it possible to use an integer variable in the loop control of a for loop? 4 answers I am trying for i in {4..100} do is_prime=true a=$(($i-1)) for divider in {2..$a} do b=$(($i error token is % $divider)) # <-- line 9 [ $b -eq 0 ] && echo 'y' #is_prime=false done [ is_prime == true ] && print "${i} is prime!" done but I get $ ./3_largest_prime.sh ./3_largest_prime.sh: line 9: 4 % {2..3}: syntax error: operand expected (error token is "{2..3}") bash scripting share|improve this question edited Jan 1 '15 at 4:35 asked Jan 1 '15 at 4:32 Michael Durrant 9,6062174118 marked as duplicate by don_crissti, jordanm, iruvar, jasonwryan, slm♦ Jan 1 '15 at 6:54 This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. add a comment| 1 Answer 1 active oldest votes up vote 2 down vote accepted If you put set -xv in your script, you might be able to detect why there is an error. In your script, it will output this line: +for divider in '{2..$a}' Notice the expansion did not occur. Read through the linked duplicates to identify how to fix it. share|improve this answer answered Jan 1 '15 at 6:45 Miati 2171410 add a comment| Not the answer you're looking for? Browse other questions tagged bash scripting or ask your own question. asked 1 year ago viewed 283 times active 1 year ago Linked 40 In bash, is it possible to use an integer variable in the loop control o