• Arun Kumar

Shell Script to Display Number in Words

Below Linux shell script allows us to convert the user input to words from numbers. Here we will be using the case statement with for loop. The bash case statement is generally used to simplify complex conditionals when you have multiple different choices.

vi ntow.sh
echo -n "Enter number: "
read n
len=$(echo $n | wc -c)
len=$(( $len - 1 ))
echo "Your number $n in words : "
for (( i=1; i<=$len; i++ ))
   # get one digit at a time
    digit=$(echo $n | cut -c $i)
   # use case control structure to find digit equivalent in words 
    case $digit in
        0) echo -n "zero " ;;
        1) echo -n "one " ;;
        2) echo -n "two " ;;
        3) echo -n "three " ;;
        4) echo -n "four " ;;
        5) echo -n "five " ;;
        6) echo -n "six " ;;
        7) echo -n "seven " ;;
        8) echo -n "eight " ;;
        9) echo -n "nine " ;;
# just print a new line
echo ""

Here is the sample output of the above script

shell script to display number in words

More ways to use the bash case statement

  • You can use it to print the official language of a given country

  • You can use the case for user YES / No as you see while software installation


Recent Posts

See All