LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Odd problem with making a variable the output of a command in a shell script (http://www.linuxquestions.org/questions/programming-9/odd-problem-with-making-a-variable-the-output-of-a-command-in-a-shell-script-392574/)

linux=future 12-13-2005 10:04 PM

Odd problem with making a variable the output of a command in a shell script
 
All I want to do is change the value of a number that was recently typed in. This is a simple script that shows exactly what I want to do:

#!/bin/bash
echo "Enter a number"
read num
num='expr $num - 1'
echo $num

Assuming the user entered 2, the output of the program, according to everything that I have read, should be 1. However, the output is expr $num - 1. I have no idea why it is doing that instead of evaluating. I know the problem is related to surrounding the expression with ''. I tried another script that used the whoami command to check to see if it was being run by root. However, 'whoami' didn't output the user name but instead whoami.

Wynd 12-13-2005 10:24 PM

Try using `...` instead of '...'

`...` means "capture the output of the command", whereas '...' means "print it out exactly as it is - no substitutions or anything."

kshkid 12-13-2005 10:25 PM

the expression should be evaluated

Code:

#!/bin/bash         
echo "Enter a number"
read num             
num=`expr $num - 1`  
echo $num


linux=future 12-13-2005 10:45 PM

Thaks Wynd! I never noticed that they always shifted to the left a bit...I better get glasses. Evaluates fine now.


All times are GMT -5. The time now is 04:29 AM.