Use exec to Redirect stdio in a git Hook Script

Cameron Nokes
InstructorCameron Nokes

Share this video with your friends

Send Tweet

Redirection like ls > out only applies to a single line, so how do we control bash's input and output streams for blocks of commands? exec does just that -- it changes where stdout and stderr go for all commands that come after it.

In this lesson, we'll use exec in a script to redirect all stdout and stderr to a log file. Our script will be a post-merge git hook, which runs after a git pull is performed. It'll check git's list of changed files and run npm install if package.json was changed. Because git operations are sometimes performed inside UI instead of the shell directly, it can be handy to capture any errors in a log file.