Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When we write shell scripts we do this:

#!/bin/bash

But What if bash is in /usr/bin/? How can we find it in a portable way inside script? I did this, but it gives error:

#!which bash
share|improve this question
up vote 9 down vote accepted

If bash is in the different location you can hash bang it as follows:

#!/usr/bin/env bash

Location for env is pretty standard across the variants.

share|improve this answer
7  
A similar arabesque is useful for python scripts: #!/usr/bin/env python – ncmathsadist Feb 19 '12 at 19:43
2  
python, perl and all other scripting languages. – Karlson Feb 19 '12 at 20:02
1  
But this method does have some drawbacks; see this question and this answer. Most systems are going to install bash as /bin/bash (via a symlink if necessary) simply because so many scripts out there have #!/bin/bash. – Keith Thompson Feb 19 '12 at 21:59
1  
There are drawbacks to using either method but the fact that scripts out there use #!/bin/bash doesn't necessarily make them right. – Karlson Feb 20 '12 at 0:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.