Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 77091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:46:33+00:00 2026-05-10T20:46:33+00:00

Is it possible to redirect all of the output of a Bourne shell script

  • 0

Is it possible to redirect all of the output of a Bourne shell script to somewhere, but with shell commands inside the script itself?

Redirecting the output of a single command is easy, but I want something more like this:

#!/bin/sh if [ ! -t 0 ]; then     # redirect all of my output to a file here fi  # rest of script... 

Meaning: if the script is run non-interactively (for example, cron), save off the output of everything to a file. If run interactively from a shell, let the output go to stdout as usual.

I want to do this for a script normally run by the FreeBSD periodic utility. It’s part of the daily run, which I don’t normally care to see every day in email, so I don’t have it sent. However, if something inside this one particular script fails, that’s important to me and I’d like to be able to capture and email the output of this one part of the daily jobs.

Update: Joshua’s answer is spot-on, but I also wanted to save and restore stdout and stderr around the entire script, which is done like this:

# save stdout and stderr to file  # descriptors 3 and 4,  # then redirect them to "foo" exec 3>&1 4>&2 >foo 2>&1  # ...  # restore stdout and stderr exec 1>&3 2>&4 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T20:46:33+00:00Added an answer on May 10, 2026 at 8:46 pm

    Addressing the question as updated.

    #...part of script without redirection...  {     #...part of script with redirection... } > file1 2>file2 # ...and others as appropriate...  #...residue of script without redirection... 

    The braces ‘{ … }’ provide a unit of I/O redirection. The braces must appear where a command could appear – simplistically, at the start of a line or after a semi-colon. (Yes, that can be made more precise; if you want to quibble, let me know.)

    You are right that you can preserve the original stdout and stderr with the redirections you showed, but it is usually simpler for the people who have to maintain the script later to understand what’s going on if you scope the redirected code as shown above.

    The relevant sections of the Bash manual are Grouping Commands and I/O Redirection. The relevant sections of the POSIX shell specification are Compound Commands and I/O Redirection. Bash has some extra notations, but is otherwise similar to the POSIX shell specification.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 73k
  • Answers 73k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Why not simply use the built in class java.util.UUID, assuming… May 11, 2026 at 1:56 pm
  • added an answer I was able to get this working by accessing the… May 11, 2026 at 1:55 pm
  • added an answer It always depends on the situation. If you KNOW there… May 11, 2026 at 1:55 pm

Related Questions

Is it possible to redirect to a different view from a controller? For example,
I'd like to know if it is possible to redirect StreamWriter output to a
Is it possible to prevent the browser from following redirects when sending XMLHttpRequest-s (i.e.
Is it possible to configure xampp to serve up a file outside of the
I am wrapping existing C++ code from a BSD project in our own custom
I have an issue whereby the company uses a business tracking system that captures
I have two servers, one in a seperate domain and the other on the
Our company has for many years had multiple domain names to protect our product

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.