Bash reverse counting with while loop


#1

Transfered from Linux Config Disqus comments:

Question:
Hi,

I have a question for my bash scripting.
Can you help me by this script?

Thanks Chris

(Modify countdown such that it prompts user for a starting value, and counts down from there.

Write a script called countdown2 that accepts the initial value as a command-line argument. For example, the command and its output might look like the following.

$ ./countdown2 12
12
11
10
9
8
7
6
5
4
3
2
1
GO!

Answer:

Probably the easiest way to do count reverse in bash is to use while loop. Save this script as countdown2, make it executable and run:

#!/bin/bash
COUNT=$1
# bash while loop
while [ $COUNT -gt 0 ]; do
        echo $COUNT
        let COUNT=COUNT-1
done
echo GO!

Output:

./countdown2 4
4
3
2
1
GO!

#2