Skip to content
Advertisement

How to use variables in shell?

Having a simple script like this, which should convert minutes to seconds

#!bin/bash
echo 'input_minutes'
read x
time="expr $x '*' 60"
echo $time

When run after entering a number (5), it outputs an expression "expr $x '*' 60" instead of a product(300). What could be the reason?

Advertisement

Answer

You didn’t execute the expression, instead you created a string. Try it like this:

time=$(expr $x '*' 60)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement