April 2, 2011

Additional note for a proper shebang

Category: Sysadmin.

I recently encountered a shell script lacking some functionality I wanted to rely on, and started editing it to provide a patch for the author.

The first line of the script was:

#!/bin/bash -x

Those who know me might imagine how my eyes were bleeding (the other may want to read my previous entry about writing portable shebang for shell scripts), but this shows another problem one may encounter when porting a shell script with an invalid shebang.

Let's assume the script has bashisms and we want to use bash to run it. According to my previous post, you may want to write this:

#!/usr/bin/env bash -x

Unfortunately, this won't work: env(1) will look for a binary names bash -x in the $PATH and fail:

env: bash -x: No such file or directory

There is however a simple solution: the set builtin(1). The right syntax is:

#!/usr/bin/env bash
set -x

No Comments Yet

Comments RSS feed | Leave a Reply…

top