Assigning output of command to a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assigning output of command to a variable
# 1  
Old 06-26-2006
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  
Old 06-26-2006
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  
Old 06-27-2006
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  
Old 06-27-2006
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  
Old 06-27-2006
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.
Smilie
# 6  
Old 06-27-2006
MySQL

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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Expect - assigning UNIX command output to a variable

Hi, I'm writing a script that connects through ssh (using "expect") and then is supposed to find whether a process on that remote machine is running or not. Here's my code (user, host and password are obviously replaced with real values in actual script): #!/usr/bin/expect set timeout 1... (3 Replies)
Discussion started by: oseri
3 Replies

2. UNIX for Dummies Questions & Answers

Tcsh command for assigning output of awk to variable

Hi I have a text file with 2 values and I am trying to assign each value to a variable and then write those to text files. So if the textfile is data.txt with 2 values x and y I want to assign mean=x, and stdev=y and then write these out in text files alongwith the id ($id has already been... (6 Replies)
Discussion started by: violin
6 Replies

3. Shell Programming and Scripting

Assigning bc output to a variable

I'm converting decimal to integer with bc, and I'd like to assign the integer output from bc to a variable 'val'. E.g. In the code below: If b is 5000.000, lines 6 and 8 will output: 5000 (5000.000+0.5)/1 | bc I'd like val to take the value 5000 though, rather than 5000.000 Does someone... (3 Replies)
Discussion started by: pina
3 Replies

4. Shell Programming and Scripting

Assigning output from awk to variable

I have a script whose contents are as below result= awk 's=100 END {print s }' echo "The result is" $result The desired output is The result is 100 My script is running without exiting and i am also not getting the desired output. Please help (5 Replies)
Discussion started by: bk_12345
5 Replies

5. UNIX for Dummies Questions & Answers

Assigning the output of a command to a variable, where there may be >1 line returned?

Hello I am using unix CLI commands for the Synergy CM software. The command basically searches for a folder ID and returns the names of the projects the folder sits in. The result is assigned to a variable: FIND_USE=`ccm folder -fu -u -f "%name"-"%version" ${FOLDER_ID}` When the command... (6 Replies)
Discussion started by: Glyn_Mo
6 Replies

6. Shell Programming and Scripting

Piping and assigning output to a variable in Perl

Hi All, I am trying to convert the below Csh code into Perl. But i have the following error. Can any expert help ? Error: ls: *tac: No such file or directory Csh set $ST_file = `ls -rt *$testid*st*|tail -1`; Perl my $ST_file = `ls -rt *$testid*st*|tail -1`; (10 Replies)
Discussion started by: Raynon
10 Replies

7. Shell Programming and Scripting

Assigning output of a command to variable

When I run time -p <command>, it outputs: real X.XX user X.XX sys X.XXwhere X.XX is seconds. How I can take just that first number output, the seconds of real time, and assign that to a variable? (9 Replies)
Discussion started by: jeriryan87
9 Replies

8. Shell Programming and Scripting

Assigning output to a variable

I am new to unix shell scripting. I was trying to convert each lines in a file to upper case. I know how to convert the whole file. But here i have to do line by line. I am getting it in the below mentioned script #!/bin/bash #converting lower to upper in a file #tr "" "" <file1... (3 Replies)
Discussion started by: jpmena
3 Replies

9. Shell Programming and Scripting

Assigning output of command to a variable in shell

hi, I want to assign find command result into some temporary variable: jarPath= find /opt/lotus/notes/ -name $jarFile cho "the jar path $jarPath" where jarPath is temporary variable. Can anybody help on this. Thanks in advance ----Sankar (6 Replies)
Discussion started by: sankar reddy
6 Replies

10. Shell Programming and Scripting

assigning command output to a shell variable

I have the sql file cde.sql with the below contents: abcdefghij abcwhendefothers sdfghj when no one else when others wwhen%others exception when others Now I want to search for the strings containing when others together and ceck whether that does not occur more than once in the... (2 Replies)
Discussion started by: kprattip
2 Replies
Login or Register to Ask a Question