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 7019759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:13:38+00:00 2026-05-27T23:13:38+00:00

I have a bash script that I want to be quiet when run without

  • 0

I have a bash script that I want to be quiet when run without attached tty (like from cron).
I now was looking for a way to conditionally redirect output to /dev/null in a single line.
This is an example of what I had in mind, but I will have many more commands that do output in the script

#!/bin/bash
# conditional-redirect.sh
if tty -s; then 
  REDIRECT=
else 
  REDIRECT=">& /dev/null"
fi
echo "is this visible?" $REDIRECT

Unfortunately, this does not work:

$ ./conditional-redirect.sh
is this visible?
$ echo "" | ./conditional-redirect.sh 
is this visible? >& /dev/null

what I don’t want to do is duplicate all commands in a with-redirection or with-no-redirection variant:

if tty -s; then 
  echo "is this visible?"
else 
  echo "is this visible?" >& /dev/null
fi

EDIT:

It would be great if the solution would provide me a way to output something in “quiet” mode, e.g. when something is really wrong, I might want to get a notice from cron.

  • 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. Editorial Team
    Editorial Team
    2026-05-27T23:13:39+00:00Added an answer on May 27, 2026 at 11:13 pm

    For bash, you can use the line:

    exec &>/dev/null
    

    This will direct all stdout and stderr to /dev/null from that point on. It uses the non-argument version of exec.

    Normally, something like exec xyzzy would replace the program in the current process with a new program but you can use this non-argument version to simply modify redirections while keeping the current program.

    So, in your specific case, you could use something like:

    tty -s
    if [[ $? -eq 1 ]] ; then
        exec &>/dev/null
    fi
    

    If you want the majority of output to be discarded but still want to output some stuff, you can create a new file handle to do that. Something like:

    tty -s
    if [[ $? -eq 1 ]] ; then
      exec 3>&1 &>/dev/null
    else 
      exec 3>&1
    fi
    echo Normal               # won't see this.
    echo Failure >&3          # will see this.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bash script that installs some software. I want to fail as
I have an xml file that I want to configure using a bash script.
I have a bash script that sources contents from another file. The contents of
I have a bash script that goes like this : # gets all relevant
I want to write a bash script that I can run in a directory
I have a bash script in a cron job that does an SVN copy
I have a bash script I'm writing that greps for a filepath. I want
I have a string in a Bash shell script that I want to split
I have this bash shell-script command that causes date to run if make succeeds
Lets assume I have a bash script that executes code like so: for i

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.