Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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)
{
    System.out.println(num)
    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, AgentCool, 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, AgentCool, 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

4 Answers 4

up vote 0 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"
done

If you must use a while loop, consider the following

i=0
while [[ $i -le 10 ]]; do
    echo "$i"
    i=$((i+=1))
done
share|improve this answer

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

num=0
while test $num -le 10; do
    echo $num
    num=$((num+1))
done

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. –  user1984712 Oct 27 '13 at 20:35
    
Oh I should add, when i execute the fileName.sh file i use bash fileName.sh. My first line of the file is #!/bin/sh. I'm not sure if any of that makes a difference –  user1984712 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
1  
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:

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

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:

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html

share|improve this answer
1  
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 –  user1984712 Oct 27 '13 at 20:45
1  
@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 tldp.org/LDP/abs/html better? –  LiveWireBT Oct 28 '13 at 7:03

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