Redirect output of a script from the inside of it

Normally, when you want to redirect (standard) output of a script to a file, you run it with a redirection, like:

$ ./script.sh > out.log

However – you can also accomplish the same from inside the script by using exec. The following will write “Test” and then current time into /tmp/out.log”

#!/bin/bash

exec >/tmp/out.log
echo Test
date

You can find more information about exec on tldp.

  1. just found a use for this. thanks! more shell tips please ;)

Leave a Comment


*


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">