The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 11:14 PM
Storing a variable? hoover90 Shell Programming and Scripting 4 01-26-2008 09:39 PM
Using 'defaults read' and storing the output in a variable davewg Shell Programming and Scripting 0 11-14-2007 08:04 AM
storing output of awk in variable mab_arif16 Shell Programming and Scripting 3 05-07-2006 06:15 PM
Storing values in variable matrixmadhan Shell Programming and Scripting 1 04-01-2005 01:56 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-08-2006
ravi raj kumar ravi raj kumar is offline
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
  #2 (permalink)  
Old 12-08-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
Code:
x='hello'
y=$(echo $x | wc -c)
  #3 (permalink)  
Old 12-08-2006
ravi raj kumar ravi raj kumar is offline
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
  #4 (permalink)  
Old 12-08-2006
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,518
can you try back quotes instead

Code:
z=`expr $y + 1`
  #5 (permalink)  
Old 12-08-2006
ravi raj kumar ravi raj kumar is offline
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
  #6 (permalink)  
Old 12-08-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
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.
  #7 (permalink)  
Old 12-08-2006
johnf johnf is offline
Registered User
  
 

Join Date: May 2006
Location: England
Posts: 284
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!!!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:32 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0