• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4123
  • Last Modified:

Unix, how to read first line in file and store in a variable

Hi Experts,
I am new to Unix.  In shell script, just trying to read first line of a  file, and store it in a variable.
 set temp =  `head -1  /test/temp.txt`
 echo $temp

But it is not working.  head -1  /test/temp.txt  works, but echo $temp does not print anything.

 WIll appreciate your help.
Thanks
0
bunker1234
Asked:
bunker1234
  • 2
  • 2
  • 2
  • +2
2 Solutions
 
omarfaridCommented:
try

temp ="`head -1  /test/temp.txt`"
echo $temp
0
 
Manoj_aCommented:
Just remove the set in your second line and try..
0
 
woolmilkporcCommented:
Hi,

temp=$(head -1 /test/temp.txt)

echo $temp

The clue is not to put spaces around the equal ( = ) sign.

The $( ) notation is equivalent to the backtick ``notation, but is POSIX compliant and far better readable.


wmp

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
rfportillaCommented:
I second that, Woolmilkporc.
0
 
bunker1234Author Commented:
Hi Experts,
Thanks so much for answering so quickly, I really appreciate it.   I wasted so many hours before posting this question on experts exchange.  Special thanks to woolmilkporc for the clue (clue  is not to put spaces around the equal ( = ) sign).  I think that was my problem.  I had already tried several ways( with set, without  set etc), but always with spaces so it did not work

Thanks again,
bunker1234
0
 
omarfaridCommented:
I think this what was shown in my posting :)
0
 
woolmilkporcCommented:
Really? :-)
0
 
bunker1234Author Commented:
Hi omarfarid,
You were the first one to post the answer.  But, without the 'no space clue' ,  I would not have noticed the space/nospace differenence.

By the way, your answer has space in it  :)
temp ="`head -1  /test/temp.txt`".  

Thanks
bunker1234
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now