top of page

Shell Script to Convert Distance Units

Linux shell script allows you to calculate the user input number to display in different units. This will be achieved by using the bc command. bc command is used for the command-line calculator.


vi convert_distance.sh
# !/bin/bash 

echo "Enter distance (in km) : "
read km

meter=`echo $km \* 1000 | bc`
feet=`echo $meter \* 3.2808 | bc`
inches=`echo $feet \* 12 | bc`
cm=`echo $feet \* 30.48 | bc`

echo "Total meter is    : $meter "
echo "Total feet is     : $feet "
echo "Total inches is   : $inches "
echo "Total centimeters : $cm "

Here is the sample output of the above script

shell script to convert distance units

More ways to use the conversion

  • You also use the if-else statement to use the unit conversion.

  • You can ask the user to make the selection of the desired conversion unit and provide the output accordingly.


Related Posts

Heading 2

Add paragraph text. Click “Edit Text” to customize this theme across your site. You can update and reuse text themes.

bottom of page