Linux, FreeBSD, Juniper, Cisco / Network security articles and troubleshooting guides

FAQ
It is currently Wed Oct 18, 2017 5:07 pm


Tutorials applicable on more than one Unix/Linux OS and shell scripts: ssh / openssl / protocols.

Author Message
debuser
Post  Post subject: bash: Redirecting standard error to a shell variable and standard output to a file  |  Posted: Mon Oct 31, 2011 6:54 am

Joined: Thu Aug 06, 2009 2:48 am
Posts: 105

Offline
 

bash: Redirecting standard error to a shell variable and standard output to a file

A little background from bashref:http://www.gnu.org/software/bash/manual/bashref.html
Quote:
3.2.4.3 Grouping Commands

Bash provides two ways to group a list of commands to be executed as a unit. When commands are grouped, redirections may be applied to the entire command list. For example, the output of all the commands in the list may be redirected to a single stream.

()
( list )

Placing a list of commands between parentheses causes a subshell environment to be created (see Command Execution Environment), and each of the commands in list to be executed in that subshell. Since the list is executed in a subshell, variable assignments do not remain in effect after the subshell completes.
{}

{ list; }

Placing a list of commands between curly braces causes the list to be executed in the current shell context. No subshell is created. The semicolon (or newline) following list is required.

In addition to the creation of a subshell, there is a subtle difference between these two constructs due to historical reasons. The braces are reserved words, so they must be separated from the list by blanks or other shell metacharacters. The parentheses are operators, and are recognized as separate tokens by the shell even if they are not separated from the list by whitespace.

The exit status of both of these constructs is the exit status of list.


In my example, the of commands is only "ls".
Code:
# ls file.txt nonexist.txt
ls: nonexist.txt: No such file or directory
file.txt


file.txt exists and nonexist.txt doesn't.

Following ERROR variable will take the value of the standard error from ls command and the standard output will be sent to output file:
Code:
ERROR=$( { ls file.txt nonexist.txt >output ; } 2>&1)
# echo $ERROR
ls: nonexist.txt: No such file or directory
# cat output
file.txt


As stated in bashref, bash shell treats the braces as reserved words and they need to be separated from the command by a space in this case.





Top
Display posts from previous:  Sort by  
E-mail friendPrint view

Topics related to - "bash: Redirecting standard error to a shell variable and standard output to a file"
 Topics   Author   Replies   Views   Last post 
There are no new unread posts for this topic. bash: -bash: output: cannot overwrite existing file

debuser

0

2128

Mon Oct 31, 2011 7:03 am

debuser View the latest post

There are no new unread posts for this topic. How to change user login shell to bash in Linux

mandrei99

0

802

Thu Jan 22, 2015 11:37 am

mandrei99 View the latest post

There are no new unread posts for this topic. Fix shell terminal after cat-ing a binary or echoing some encrypted content

admin

0

375

Thu Mar 03, 2011 5:58 am

admin View the latest post

There are no new unread posts for this topic. Daemontools /service/test/log: unable to open supervise/ok: file does not exist

debuser

0

1848

Thu Nov 26, 2009 7:11 am

debuser View the latest post

There are no new unread posts for this topic. Linux - Unable to login (and authentication succeeds) - File size limit exceeded

debuser

3

583

Tue Dec 06, 2011 8:24 am

admin View the latest post

There are no new unread posts for this topic. Normalizing terminal prompt (or putty prompt) after catting a binary file

debuser

2

628

Mon Jul 29, 2013 1:02 am

jonnblaze View the latest post

 

Who is online
Users browsing this forum: No registered users and 0 guests
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum
Jump to:  
News News Site map Site map SitemapIndex SitemapIndex RSS Feed RSS Feed Channel list Channel list


Delete all board cookies | The team | All times are UTC - 5 hours [ DST ]



phpBB SEO