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

  • Home
  • SEARCH
  • 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 8764881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:08:37+00:00 2026-06-13T16:08:37+00:00

I need to write the time taken to execute this command in a txt

  • 0

I need to write the time taken to execute this command in a txt file:

time ./program.exe

How can I do in bash script?

I try with >> time.txt but that doesn’t work (the output does not go to file and does go to the screen).

  • 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-13T16:08:38+00:00Added an answer on June 13, 2026 at 4:08 pm

    Getting time in bash to write to a file is hard work. It is a bash built-in command. (On Mac OS X, there’s an external command, /usr/bin/time, that does a similar job but with a different output format and less recalcitrance.)

    You need to use:

    (time ./program.exe) 2> time.txt
    

    It writes to standard error (hence the 2> notation). However, if you don’t use the sub-shell (the parentheses), it doesn’t work; the output still comes to the screen.


    Alternatively, and without a sub-shell, you can use:

    { time ./program.exe; } 2> time.txt
    

    Note the space after the open brace and the semi-colon; both are necessary on a single line. The braces must appear where a command could appear, and must be standalone symbols. (If you struggle hard enough, you’ll come up with ...;}|something or ...;}2>&1. Both of these identify the brace as a standalone symbol, though. If you try ...;}xyz, the shell will (probably) fail to find a command called }xyz, though.)


    I need to run more command in more terminal. If I do this:

     xterm -xrm '*hold: true' -e (time ./Program.exe) >> time.exe & sleep 2
    

    it doesn’t work and tells me Syntax error: "(" unexpected. How do I fix this?

    You would need to do something like:

    xterm -xrm '*hold: true' -e sh -c "(time ./Program.exe) 2> time.txt & sleep 2"
    

    The key change is to run the shell with the script coming from the argument to the -c option; you can replace sh with /bin/bash or an equivalent name. That should get around any ‘Syntax error’ issues. I’m not quite sure what triggers that error, though, so there may be a simpler and better way to deal with it. It’s also conceivable that xterm‘s -e option only takes a single string argument, in which case, I suppose you’d use:

    xterm -xrm '*hold: true' -e 'sh -c "(time ./Program.exe) 2> time.txt & sleep 2"'
    

    You can manual bash xterm as well as I can.

    I’m not sure why you run the timed program in background mode, but that’s your problem, not mine. Similarly, the sleep 2 is not obviously necessary if the hold: true keeps the terminal open.

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

Sidebar

Related Questions

I need to write a program that takes input line at a time and
I need to have a newline every time I write to a file in
i need horizontal scrolling script in javascript, but don't have much time to write
I need to write ranks[a], ranks[b], count to a file, each time on a
I need to write a program where during run time, a set of integers
I need to write the local time information , the exception message to a
I need to use the clock on my SQL Server to write a time
If we must connect the db (redis)every time we need to write to or
The application need write file's last modification date. void Dater(String DateFile) { File file
I need to write a script in Matlab, which will read some data from

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.