Bash – integer expression expected bash

bashscripting

Trying to loop a phrase inputted by the user a set amount of times that is also inputted by the user. Keep on getting the error integer expression expected, and I can't figure out how to fix it.

#!/bin/sh 
echo "What do you want to say?"
read phrase 

echo "How many times?"
read num

while [ "num" -ge 0 ]
do
     echo $phrase
     num='expr num - 1'
done

Best Answer

You need to use a $ to expand a variable in bash, unless it's inside of (( )). You are comparing the literal string "num" to the number 0. Use one of the following:

while [ "$num" -ge 0 ] # POSIX
while (( num >= 0 )); # bash
Related Question