Logging to the console and a file from a Bash script


We’re going to provide a drop in script component that will allow you to log output (stdout and stderr) to a file, as well as the screen. In a follow-up, we’ll show how this can also be run through a filter so that your output could be timestamped. We’ll also put in some simple error handling to ensure the logging destination exists.

Script with log


Place this at the top of your script

exec &> >( tee "$LOGFILE" )


$LOGFILE=output.log ./script.sh

How it works

This is a pretty dense line. Here are the concepts:

exec &>

Redirects the stderr & stdout of the entire script. man exec `&>` is shorthand for redirecting both stderr and stdout.

>( ... )

The process (the command in parenthesis) receives its input from exec (stdin & stderr) Read more on Process Substitution


Tee is one of those rare bash commands with a name that actually makes sense. It reads from standard input, and writes it to standard output and files. It basically takes input from 1 place, and can write it to multiple output destination. man tee


Shell Parameter Expansion Shell parameter expansion doesn’t have to use {}, and in this case it shouldn’t matter, but it’s a best practice.  The quotes will make sure that file names with a space won’t break the logging

I’d like to call out Michael Hormer’s answer as being especially instructive as I was sorting this out.


How to (productively) use 15 minutes of time

all-waiting-at-airportI’ve got 2 little boys at home.  My wife and I enjoy spending a lot of time with them.  We’re also jumping into the real estate game, since that seems to be the way to achieve financial independence for our generation.  Adding to that a full time job and exercise, and my free time is in pretty short supply.
That doesn’t leave much room for my pre-family interests. Software development and entrepreneurship in my case. I’ve been struggling with how to keep those interests alive in some form. Here’s a little of what I’ve learned so far

First, the easy stuff:

Exercise & Sleep

Both of these are critical, and I’ll not regurgitate the endless amount of research that has proven it to be so. If you haven’t noticed how much smarter you are when you’re healthy and well-rested, then keep a diary for a month, and rate your productivity 1-10, and along side that, note how much sleep and exercise you got that day. That ought to be sufficient to prove this to you.

Yes, this cuts even further into the amount of time you have. I didn’t say it wasn’t tough. Just necessary. In addition to being stronger and with more endurance, you’ll be happier, smarter and have more will power than before. (Time permitting, I’ll see if I can find the various sources to cite.)

Seriously, exercise is probably the most powerful drug out there. Take it.

Time Management

When you’re dealing with small blocks of non-contiguous time, you must set realistic, specific goals.  Gone are the days where you can say: “This weekend, I’ll learn X”.  That’s frustrating, but you can focus more intensely when you know you only have a little time.

For me, the trick seems to be to slice my tasks to fit these meager time blocks.  Ruthlessly. If you have to scope your work down to 15 minute intervals (I can already hear my youngest starting to whine), you’ll probably find that you can’t just create your typical todo lists. For instance, I broke this blog post into 3 parts:

  1. Draft 1st post on 15 minutes slices, pt1
  2. Draft 1st post on 15 minutes slices, pt2
  3. Complete and publish 1st post on 15 minutes slices

This might seem excessive, but it’s really important.  It can be really easy to feel like you’re not making any progress.  Giving yourself these tiny deliverables, and then checking them off helps you in 2 ways:

  1. It makes you FEEL accomplished. The act of checking something off is powerful, and keeps you moving
  2. When you have a short period of time separated by hours or days from your last period, you can easily waste a lot of it trying to figure out what to do. Next thing you know, you’ve done nothing. Frustrating! Having the list of tasks that are SUPER focused ahead of time means that you’ve put a lot of the context of your problem / goal into your task list. So you can pick it up and go quicker.
  3. “You have 15 minutes to write this task: Go!”  There’s power in deadlines.  Impose them on yourself.  Force yourself to go faster than you’re comfortable going.  You’ll get more done.

Realize that small efforts every day are habit-forming.  Aristotle said: “We are what we repeatedly do. Excellence, then, is not an act, but a habit.”


© 2015 Jeff Deville

Theme by Anders NorenUp ↑