0

I am beginner in UNIX. I am getting a silly error while writing a while loop.

Code:-

$ x=0
$ while [ $x -lt 10 ]
> do
> echo $x
> x=´echo "$x + 1" | bc´
> done;

I am getting the errors:-

0
bc´: command not found   
0
bc´: command not found
0
bc´: command not found
...

Can any body help me? I have no idea of shell programming.

3
  • please share complete program. Commented May 11, 2013 at 18:46
  • Consider using let for arithmetic operations i.e. while [ $x -lt 10 ]; do echo $x; let x=x+1; done Commented May 11, 2013 at 18:48
  • @another.anon.coward As long as you're using bash, might as well use (( )) instead of let. Commented May 11, 2013 at 18:51

4 Answers 4

1

If you are doing x=´echo "$x + 1" | bc´ to increment x (which is wrong as pointed by danf), pls use the following

x=`expr $x + 1`

also note the spaces...bash is very picky

Here is the output --

xxxx@cse:~> x=5
xxxxx@cse:~> while [ $x -lt 10 ]; do echo $x; x=`expr $x + 1`; done;
5
6
7
8
9

You can use bc to get this to work, but it is better to use expr

xxxx@cse:~> x=5
xxxx@cse:~> while [ $x -lt 10 ]; do echo $x; x=`echo "$x + 1"|bc`; done;
5
6
7
8
9
Sign up to request clarification or add additional context in comments.

5 Comments

Or x=$(($x + 1))...or variants on that theme, including : $((x++))...I agree: built-in arithmetic would be better, but the bc code can be made to work too.
thanks for reply. but not working. while [ $x -lt 10 ]; do echo $x; x=´expr $x +1´; done;bash: [: too many arguments
@JonathanLeffler That will also work, but I personally like expr
@Bit_hunter You have your ticks wrong,,,it is the key below your ESC key, <backquotes> not '
@Bit_hunter I am guessing you are running from prompt, so your x is not getting initialized....before the while, add x=0 or whatever initialization you want. Please see my answer, I added some examples for you. Also note that expr $x +1 is WRONG, it has to be expr $x + 1 (note the space before 1)
0

You seem to have a parse error. You need a backquote. Change line to:

x=`echo "$x + 1" | bc`

Comments

0

Don't use backticks to execute subcommands, use $( cmd ), this construct can be nested. Maybe you do the arithmetic with a pipe to bc for learning purposes, otherwise, the shell is capable of doing this in a number of ways

$((x+=1))
x=$((x+1))
$((++x))
$((x++))

HTH and kind regards

3 Comments

Just going to point out that the OP didn't say anything about bash, and your answer is composed entirely of bash-specific solutions. While this will probably work, it's by no means guaranteed.
I can't check right now, but I'm quit sure zsh can do the same. And while you are right, of course, the probability that the OP is using bash is high :-)
I'm sure zsh can do it as well, because it's basically an extension of bash. What I meant was, this is not POSIX compliant. So this doesn't work in sh or dash.
0

You're not using backticks. Use ` (aka grave accent, aka U+0060) (found in the top-left on American keyboards) instead of ´ (aka acute accent, aka U+00B4).

For example, the following works fine:

x=0
while [ $x -lt 10 ]; do
  echo $x
  x=`echo "$x + 1" | bc`
done

The only difference between yours and mine are the ticks used to quote echo "$x + 1" | bc.

That being said, if you happen to be using bash (or a bash-like shell), there are much better ways of making the same loop. For example:

x=0
while (( x++ < 10  )); do
  echo $x
done

This has the advantage of being both faster (because it doesn't call to outside programs) and easier to read (because it uses more traditional coding syntax).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.