The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Storing count(*) into unix variable mervinboyz Shell Programming and Scripting 1 03-26-2008 08:14 PM
Storing a variable? hoover90 Shell Programming and Scripting 4 01-26-2008 06:39 PM
Using 'defaults read' and storing the output in a variable davewg Shell Programming and Scripting 0 11-14-2007 05:04 AM
storing output of awk in variable mab_arif16 Shell Programming and Scripting 3 05-07-2006 03:15 PM
Storing values in variable matrixmadhan Shell Programming and Scripting 1 03-31-2005 10:56 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 12-08-2006
Registered User
 

Join Date: Dec 2006
Location: hyderabad
Posts: 102
Storing the output into a variable

Hi unix gurus,

I am trying to store the result of a command into a variable.
But it is not getting stored.

x='hello'
y=echo $x | wc -c

but it is giving the output as 0(zero)

Pls help me its very urgent
Reply With Quote
Forum Sponsor
  #2  
Old 12-08-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
Code:
x='hello'
y=$(echo $x | wc -c)
Reply With Quote
  #3  
Old 12-08-2006
Registered User
 

Join Date: Dec 2006
Location: hyderabad
Posts: 102
Ok,fine .Its working.But when i am trying to use the result in another calculation its not giving the proper result.
x='hello'
y=$(echo $x | wc -c)
echo $y
z='expr $y + 1 '
echo $z

Its printing expr $y + 1 instead of 7.

any idea pls.



cheers
Ravi Raj kumar
Reply With Quote
  #4  
Old 12-08-2006
Registered User
 

Join Date: Sep 2006
Posts: 1,580
can you try back quotes instead

Code:
z=`expr $y + 1`
Reply With Quote
  #5  
Old 12-08-2006
Registered User
 

Join Date: Dec 2006
Location: hyderabad
Posts: 102

Thank u so much boss.Its working now fine.




Thanks a lot.








cheers
Ravi raj kumar
Reply With Quote
  #6  
Old 12-08-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
You need to understand the difference between

y=$(echo $x | wc -c)
and
z='expr $y + 1 '

In y=$(echo $x | wc -c), the output of the command echo $x | wc -c is stored into y. It is known as command substitution. You can do a command substitution in two ways. Either use $(...) or `...`

In z='expr $y + 1', the string expr $y + 1 is stored in z without any literal expansion.
Reply With Quote
  #7  
Old 12-08-2006
Registered User
 

Join Date: May 2006
Location: England
Posts: 265
Quote:
Originally Posted by ravi raj kumar
Ok,fine .Its working.But when i am trying to use the result in another calculation its not giving the proper result.
x='hello'
y=$(echo $x | wc -c)
echo $y
z='expr $y + 1 '
echo $z

Its printing expr $y + 1 instead of 7.

any idea pls.



cheers
Ravi Raj kumar
The script works if you have the correct quote marks around the z='expr $y + 1 '. It is not the single quote ' but the backward quote` very important! I have tested it just to make sure!!!
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:02 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0