Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 11-09-2006, 03:50 AM   #1
Registered: May 2006
Posts: 55

Rep: Reputation: 15
storing output of sed in a variable in shell script

Hi friends,

How to store output of sed command in a variable in shell script???

I tried:

1. ls -l >filenames
2. var=`sed -n '5p' filenames` #5th line of the file
3. echo $var

It is not working.

Please help..........

Thanks in advance.
Old 11-09-2006, 03:57 AM   #2
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
$ cat file.txt
this is line 1 of file.txt
this is line 2 of file.txt
this is line 3 of file.txt
$ var=`sed -n '2p' file.txt`
$ echo $var
this is line 2 of file.txt
Seems to work ok to me. Perhaps your ls -l command doesn't produce 5 lines?


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with sed n variable in shell script dipenchaudhary Programming 4 02-12-2006 10:54 PM
Odd problem with making a variable the output of a command in a shell script linux=future Programming 3 12-13-2005 09:45 PM
sed doesn't accept $variable in bash script chii-chan Programming 6 05-28-2005 07:07 AM
insert output of sed into a variable hwouters Linux - General 3 11-06-2004 07:54 PM
Expanding shell variable in sed goofyheadedpunk Programming 4 09-11-2004 05:17 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:20 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration