![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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!! |
|
||||
|
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. ![]() |
|
||||
|
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. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|