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 am new to shell scripting so I am curious about using While Loops in Shell Script form.

I know how to declare a variable and use a while loop in java

int num = 0;
while(num <= 10)
    num = num + 1

But I don't know how to declare variables and use them in a while loop in a Shell Script.

share|improve this question

closed as off-topic by Braiam, Eric Carvalho, Raja, Jorge Castro, Andrea Corbellini Oct 28 '13 at 17:13

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This is not about Ubuntu. Questions about other Linux distributions can be asked on Unix & Linux, those about Windows on Super User, those about Apple products on Ask Different and generic programming questions on Stack Overflow." – Braiam, Eric Carvalho, Raja, Jorge Castro, Andrea Corbellini
If this question can be reworded to fit the rules in the help center, please edit the question.

Mm... why a debian tag? – Braiam Oct 28 '13 at 0:54
@Braiam The tag could be removed, this is a general programming question (it could also be posted on SO) – minerz029 Oct 28 '13 at 2:43
up vote 1 down vote accepted

You'll be much happier using a for loop instead (according to your example)

for i in {0..10}; do
    echo "$i"

If you must use a while loop, consider the following

while [[ $i -le 10 ]]; do
    echo "$i"
share|improve this answer

There are various ways of doing this, but the shell script which most closely mirrors your Java example is this:

while test $num -le 10; do
    echo $num

A more 'shell-ish' ways to achieve the same would be

printf '%s\n' {0..9}

But note that the {0..9} construction is a 'bashism', which will not work in the standard (POSIX) shell.

share|improve this answer
I used this example and I got an error. "line 4: num: command not found. – Schmidty15 Oct 27 '13 at 20:35
Oh I should add, when i execute the file i use bash My first line of the file is #!/bin/sh. I'm not sure if any of that makes a difference – Schmidty15 Oct 27 '13 at 20:39
@user1984712 You got "command not found" because you added a space between num and the = sign. – zwets Oct 27 '13 at 20:45
No, in bash we would do this: printf '%s\n' {0..9}. – gniourf_gniourf Oct 27 '13 at 20:46
@gniourf_gniourf That actually looks better than the echo, I'll add it. – zwets Oct 27 '13 at 20:50

Yet another simple way:

while (($num <= 10)); do
    echo "$num"

This will work in bash, but not in a standard POSIX shell.

share|improve this answer

This Bash Scripting site should help you, generally you don't have to declare variables before you use them:

share|improve this answer
Oh dear, please don't link to such horrible documents! This document really shows what you should not do rather than showing good practices! – gniourf_gniourf Oct 27 '13 at 20:44
Whats wrong with it? I was just going to say that it worked – Schmidty15 Oct 27 '13 at 20:45
@user1984712 What's wrong? Parsing output of ls, lacks of quotes, outdated C-loop style, use of capitalized variable names, use of let instead of bash's arithmetic, use of [] instead of [[...]] or of bash's arithmetic, ... please, just don't read this document, unless you want to write crappy code and not learn proper bash. – gniourf_gniourf Oct 27 '13 at 20:50
@gniourf_gniourf if it's that bad, you should downvote it. – Braiam Oct 28 '13 at 2:52
Hm, that wasn't updated since 2000. Is better? – LiveWireBT Oct 28 '13 at 7:03

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