1

I'm trying to run a very simple script that reads input from a user and continuously loop.But they show error [:too many arguments

i=0
while [ $i -le 5 ]
do
echo $i
i='expr $i + 1'
done

2 Answers 2

3

Change '' to Acute (`). I have tested it works fine

i=0
while [ $i -le 5 ]
do
echo $i
i=`expr $i + 1`
done
Sign up to request clarification or add additional context in comments.

3 Comments

What's a "teldi"? If you meant to write "tilde", then that's another character: the ~.
oh yeah thanx.. I meant Acute( ' ) not tilde
Actually it is, if anything, a grave accent rather than an acute accent. Whether or not the grave accent and the back-quote are the same character or not is subject to some debate.
2

The error is that you're using single-quotes instead of back-quotes for your command-substitution. In other words, it should be i=`expr $i + 1` rather than i='expr $i + 1'.

Also, an @Pankrates points out, it may be more readable to use the equivalent expression i=$(expr $i + 1). Had the author of whatever material you were reading used that form, it seems safe to say that your troubles had been avoided, at least.

3 Comments

or preferably replace the outdated backticks with $()
@Pankrates Do you have any reason for saying that backticks are "outdated"? It may or may not be argued that $() is more readable (and I'll edit in a note on this, thanks), but I haven't heard anything about them being considered obsolescent.
You are correct, both methods are still supported (and POSIX compliant) but $() has become the more preferable option because of its better nestability. see this bash FAQ

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.