Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


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

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


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:


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
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:


where foo is your variable.
Old 01-20-2007, 03:41 AM   #3
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:
foo=$(command 2>&1)
Old 01-20-2007, 04:51 AM   #4
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.



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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:32 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