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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:25:34+00:00 2026-06-05T11:25:34+00:00

I want to direct the output of a printf in a bash script to

  • 0

I want to direct the output of a printf in a bash script to stderr instead of stdout.

I am not asking about redirecting either stderr or stdout from where ever they are currently routed. I just want to be able to send the output from a printf to stderr instead of to the default of stdout.

I experimented a little and found that appending 1>&2 to the printf, as shown in the example below, appears to do what I want. However, I have no experience using bash. So my primary question is if there is a “better” way to do this in bash?

By “better” I mean is there another way to do this which is more commonly used, more conventional, or more idiomatic? How would a more experienced bash programmer do it?

#!/bin/bash
printf "{%s}   This should go to stderr.\n" "$(date)" 1>&2 
printf "[(%s)] This should go to stdout.\n" "$(date)" 

I also have a secondary question. I am asking it not so much because I need to know, but more because I am just curious and would like to have a better understanding about what is happening.

It seems the above will only work when it runs inside a shell script. It does not appear to work when I try it from a command line.

Here is an example of what I mean.

irrational@VBx64:~$ printf "{%s} Sent to stderr.\n" "$(date)" 1>&2 2> errors.txt
{Sat Jun  9 14:08:46 EDT 2012} Sent to stderr.
irrational@VBx64:~$ ls -l errors.txt
-rw-rw-r-- 1 irrational irrational 0 Jun  9 14:39 errors.txt

I would expect the printf command above to have no output because the output should go to stderr, which in turn should go to a file. But this does not happen. Huh?

  • 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-06-05T11:25:37+00:00Added an answer on June 5, 2026 at 11:25 am

    First, yes, 1>&2 is the right thing to do.

    Second, the reason your 1>&2 2>errors.txt example doesn’t work is because of the details of exactly what redirection does.

    1>&2 means “make filehandle 1 point to wherever filehandle 2 does currently” — i.e. stuff that would have been written to stdout now goes to stderr. 2>errors.txt means “open a filehandle to errors.txt and make filehandle 2 point to it” — i.e. stuff that would have been written to stderr now goes into errors.txt. But filehandle 1 isn’t affected at all, so stuff written to stdout still goes to stderr.

    The correct thing to do is 2>errors.txt 1>&2, which will make writes to both stderr and stdout go to errors.txt, because the first operation will be “open errors.txt and make stderr point to it”, and the second operation will be “make stdout point to where stderr is pointing now”.

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

Sidebar

Related Questions

Instead fo displaying in alert box I want to direct my output from javascript
I want to protect a directory and the php files in it, from direct
I want to direct a customer in an e-commerce site to pay via Paypal's
in linux, let say i want to start tomcat, and want to direct all
I have a Netflix Affiliate account, but I don't want to direct users to
I have a file with .php extention www.example.com/thefile.php?name=123 that I want to direct the
I have a form, when user submit the form, I want to direct the
I want send a direct message to one of my follower of twitter through
I have a PHP script that produces a .CSV export file from a MySQL
I was trying to just output my request with just a direct write to

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.