4

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.

munish
  • 7,987

2 Answers2

7

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.

  • 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
  • Also, if you want to specify some option to expect, like -f, the following syntax seems to work: #! /usr/bin/env -S expect -f (found this in one of the answers in https://unix.stackexchange.com/questions/63979/shebang-line-with-usr-bin-env-command-argument-fails-on-linux) – Attila123 Jan 22 '23 at 22:15
3

I suggest

#!/bin/sh
expect_path="$(which expect)"
"$expect_path" "$0" "$@"

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

Hauke Laging
  • 90,279
  • 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