Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to make a while...loop statement in my Shell script like:

while [ $line < $line_number ]; do
    echo $line

But there is an error. I think it is because I wrote it in bash code. But I need to do it in sh mode. How can I do it? Thanks!

share|improve this question
"But there is an error." What error? Do you mean an error message, or just that something doesn't work as you intended? What happens exactly? And how are you running your script? I recommend editing your question to include all this information. – Eliah Kagan Feb 26 '14 at 19:00
OK, I will edit my question. Yes, I have an error message. – Ziyaddin Sadigov Feb 26 '14 at 19:11
up vote 4 down vote accepted

You need to use, either

while [ $line -lt $line_number ]; do
    echo $line


while [[ $line < $line_number ]]; do
    echo $line


Single [] are posix shell compliant condition tests.

Double [[]] are an extension to the standard [] and are supported by bash and other shells (e.g. zsh, ksh). They support extra operations (as well as the standard posix operations). For example: || instead of -o and regex matching with =~. A fuller list of differences can be found in the bash manual section on conditional constructs.

Use [] whenever you want your script to be portable across shells. Use [[]] if you want conditional expressions not supported by [] and don't need to be portable.

share|improve this answer
Yes, it worked with -le. Thanks! – Ziyaddin Sadigov Feb 26 '14 at 19:11
@ZiyaddinSadigov plz see the edit, it will be -lt which is equivalent to <, -le means <= – souravc Feb 26 '14 at 19:21

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.