Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


LinkBack Search this Thread
Old 12-13-2005, 09:04 PM   #1
Registered: Apr 2005
Distribution: Debian
Posts: 154

Rep: Reputation: 30
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:

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.
Old 12-13-2005, 09:24 PM   #2
Registered: Jul 2001
Distribution: Slackware 12
Posts: 511

Rep: Reputation: 32
Try using `...` instead of '...'

`...` means "capture the output of the command", whereas '...' means "print it out exactly as it is - no substitutions or anything."
Old 12-13-2005, 09:25 PM   #3
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
the expression should be evaluated

echo "Enter a number" 
read num              
num=`expr $num - 1`   
echo $num
Old 12-13-2005, 09:45 PM   #4
Registered: Apr 2005
Distribution: Debian
Posts: 154

Original Poster
Rep: Reputation: 30
Thaks Wynd! I never noticed that they always shifted to the left a bit...I better get glasses. Evaluates fine now.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script and Dynamic variable xanthium Programming 11 07-12-2011 06:05 AM
Problem executing mv command inside shell script pablogosse Linux - General 7 12-16-2009 12:21 PM
Shell Script Random Variable Daniel Programming 14 12-03-2007 05:00 AM
shell scipting: append output of a command to a variable jonhewer Linux - Newbie 10 08-24-2005 05:42 AM
Assigning the output of one command to a variable (shell) guru_stew Programming 5 08-03-2003 06:12 PM

All times are GMT -5. The time now is 10:48 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration