LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-20-2007, 03:04 AM   #1
Rostfrei
Member
 
Registered: Aug 2005
Location: Slovenia
Distribution: Fedora, Ubuntu
Posts: 69

Rep: Reputation: 15
How to redirect streams to the variable?


Hello!

I know how to redirect stdout or stderr streams to the files or null device like

ls some_file 1>result_file 2>&1


Now I want to do something similar in the shell scrips and I want the resulting streams to go in some local shell script variable. How do I do it?

I would like something like:

#!/bin/sh

result_var=0
ls some_file 1>result_var 2>&1
echo $result_var


Best regards,

Last edited by Rostfrei; 01-20-2007 at 03:05 AM.
 
Old 01-20-2007, 03:09 AM   #2
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
Two ways:

foo=`command`
foo=$(command)

where foo is your variable.
 
Old 01-20-2007, 03:41 AM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,784

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
If you want the error output in the variable, you can simply add it like this:
Code:
foo=$(command 2>&1)
 
Old 01-20-2007, 04:51 AM   #4
Rostfrei
Member
 
Registered: Aug 2005
Location: Slovenia
Distribution: Fedora, Ubuntu
Posts: 69

Original Poster
Rep: Reputation: 15
Thank you jlliagre and btmiller. Both versions work flawlessly. This is exactly what I needed.

Regards,
 
  


Reply


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
setting a variable variable in a script... this works, but could it be more elegant? pwc101 Programming 3 08-18-2006 12:23 PM
what are streams? kapilcool Linux - Software 1 05-11-2006 11:50 AM
Scripting: accessing a variable stored in a variable? tomolesonjr Linux - Newbie 5 05-05-2006 09:47 PM
Retrieving from a variable whose name is inside a variable. thekillerbean Linux - General 4 02-09-2006 09:50 PM
How to get 'Window' variable from a 'Widget' variable bordel Programming 0 11-19-2003 04:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:32 AM.

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