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

I am writing expect script which can be used on mulple operating systems but the problem is

i can't use #!/usr/bin/expect evreywhere so instead i tried to do

 #!`which expect`

at the top but it failed

[sesiv@itseelm-lx4151 ~]$ ./sendcommand
-bash: ./sendcommand: `which: bad interpreter: No such file or directory

any solution for this.

share|improve this question
Is there anything you can use everywhere, /bin/bash e.g.? – Hauke Laging Apr 24 '13 at 14:29
i am not sure about this but, i think most standard OS will have /bin/sh say AIX,solaris,linux,redhat,suse,HP – munish Apr 24 '13 at 14:41
am i right ? @Hauke – munish Apr 24 '13 at 14:41
up vote 4 down vote accepted

One trick that mostly works (for perl, python, php interpreters, and probably others):

#!/usr/bin/env expect

I think env is always in /usr/bin/. A lot of interpreters can run that way now. Other hacks used to exist, but weren't understandable, or weren't all that portable.

share|improve this answer
This is also the portable way to run bash (#!/usr/bin/env bash, which will work even when your bash is in /usr/bin/local/bash or elsewhere). – Hennes Apr 24 '13 at 16:27

I suggest

expect_path="$(which expect)"
"$expect_path" "$0" "$@"

I hope this works; I am not familiar with the differences between bash and sh.

share|improve this answer
You can also expand on this by putting a check that it was actually found, and using exec before the path. – Keith Apr 24 '13 at 21:47

Your Answer


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.