Simple tips when writing scripts

Be it bash, tcsh, perl, ruby, python, etc script, here’s my one simple tips for you:

  • Do not use direct path to the program path in the script’s shebang line – use /usr/bin/env <program> instead.

It’s foolish to assume that everyone have bash in /bin/bash (and it’s even worse if you assumed /bin/sh = bash and recent version of bash will fallback to POSIX feature subset when executed using /bin/sh). And it doesn’t make sense to assume every single perl/ruby/python/etc main binary is in /usr/bin/{perl,ruby,python}.

Instead of doing that, just use /usr/bin/env to let the shell decide where to find specified program. For example if you’re writing bash script, put

#!/usr/bin/env bash


This will ensure [1] no one will went RAGE when using your script. I know I did when seeing such scripts. Or as previously stated, #!/bin/sh as the shebang and somewhere in the script it uses [[. Totally (un)funny.

[1] Basically /usr/bin/env will execute the script using program specified in its argument based on PATH specified in the environment variable. It doesn’t matter where the actual binary is located as long it’s in the PATH.

Leave a Reply

Your email address will not be published. Required fields are marked *