The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Assigning output to a variable jpmena Shell Programming and Scripting 3 03-27-2008 04:39 AM
Assigning output of command to a variable in shell sankar reddy Shell Programming and Scripting 6 02-28-2008 03:01 AM
assigning nawk output to shell variable user_prady Shell Programming and Scripting 6 11-29-2007 04:01 AM
assigning command output to a shell variable kprattip Shell Programming and Scripting 2 07-09-2007 05:01 AM
Command output to a variable. videsh77 Shell Programming and Scripting 8 12-16-2004 06:06 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 06-26-2006
oma04 oma04 is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 3
Assigning output of command to a variable

Hi,

I'm trying to assign the output of a command to a variable and then concat it with another string, however, it keeps overwriting the original string instead of adding on to the end of the string.

Contents of test.txt --> This is a test

var1="`head -n 1 test.txt`"
echo $var1 (This is a test)

var1=$var1"123"
echo $var1 (123s is a test [instead of: This is a test123)

The issue has something to do with the first line. If I simply assign a string like this:
var1="This is a test" , then it all works, but when I try to assign the output of a command to a variable, it does not add to the string properly.

Does anyone know how to get around this??

Thanks!!
  #2 (permalink)  
Old 06-26-2006
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
Try it like this:

Code:
/tmp$ cat test.txt
This is a test
This is also a test
/tmp$ var1="`head -n 1 test.txt`"
/tmp$ echo $var1
This is a test
/tmp$ var1=${var1}123
/tmp$ echo $var1
This is a test123
/tmp$

  #3 (permalink)  
Old 06-27-2006
LivinFree's Avatar
LivinFree LivinFree is offline Forum Advisor  
Goober Extraordinaire
  
 

Join Date: Jul 2001
Location: Portland, OR, USA
Posts: 1,584
Modern Bourne shell variants support $(command) notation as well, which allows nesting and, in my opinion, is easier on the eyes.

Some, such as bash, allow you to do fun stuff such as this=$(<file.txt) to grab the contents of file.txt in the variable "this".
  #4 (permalink)  
Old 06-27-2006
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
I agree there, but I used the back-quotes as that's what the OP used while asking the question. You *can* nest commands using backquotes as well, but the number of escape chars would increase with every level.
  #5 (permalink)  
Old 06-27-2006
oma04 oma04 is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 3
Thanks for both of your replies.

blowtorch - I tried the code you have given, but ended up with the same result --> 123s is a test

I am using Korn Shell which comes with the Microsoft Services for Unix Applications (SUA) add on component to Windows Server 2003 R2.

Is there a command which I can use to insert a string into the other string - either in the middle or at the end? For example, it would count the number of characters in "This is a test" and then append after then 14th character.
  #6 (permalink)  
Old 06-27-2006
oma04 oma04 is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 3
Thumbs up

Found the issue!

As always, with these types of problems, it was some minor annoyance that was causing it - a carriage return/line feed in the test.txt file after the string.

If I do a wc -c on the file it would give me 16 instead of 14 (2 extra for the CRLF).

Thanks for your help!

I've changed my code to use the var1=${var1}123 (thanks blowtorch) notation and also the var1=$(<test.txt) (thanks macosta) notation, which looks cleaner.
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 03:19 AM.


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