Celeb Glow
general | March 27, 2026

Bash convert string to integer

I am new at bash scripting, I need to write a script capable of taking a file with a given text (written numbers and arithmetic operators) and transform these words into integers and ex: +, -, *, %.

The example that I have is a file with the following text : one two three plus nine eight seven. The script needs to change these words and transform them to 123 + 987, then printing the result 1110.

So far I have this, but I am not sure how to compare each word in the file to the ones in my code with the correct values to be able to do the operations.

#!/bin/bash
fileCal="$1" #file in_calc is entered
d=( ["zero"]="0" ["one"]="1" ["two"]="2" ["three"]"3" ["four"]="4"
["five"]"5" ["six"]="6" ["seven"]="7" ["eight"]="8" ["nine"]="9" ["plus"]="+" ["minus"]="-"
["times"]="\*" ["div"]="/" ["modulo"]="%")
if [ "$#" == 0 ]; then echo $error
elif [ -f $fileCal ]; then read -a $fileCal
fi
Reset to default

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy