Why is my expect script failing on line 1?

I had this issue and found I didn't have the expect interpreter installed! Oddly enough, if you ran the command in the shell it worked. However, through a shell script I got this error:

/usr/bin/expect: bad interpreter: No such file or directory

I fixed it by simply installing the Expect interpreter. The package name that was chosen was: expect libtcl8.6

Just run:

sudo apt-get install expect

Tried it and here is the result: /usr/bin/expect^M: bad interpreter

Is it possible that there's a Windows newline (the ^M) in there that's confusing the script? You can try od to see what newline character(s) is after the expect and tofromdos or an editor (e.g. emacs in hexl-mode) to remove it. See the man pages for more info.


I don't really know expect, to be honest, but when I run that on my system it "works" fine. Nothing happens, but that's what I'd expect. I don't get any error message. According to the man page,

#!/usr/bin/expect -f

is the correct way to start your script. Expect then slurps up the script you are executing as the cmdfile.

The way I got it to reproduce the problem was to actually put a ^M at the end of the line instead of a normal newline (saw Bert F's response and that prompted me to try it). I'm sure vim's :set list command will show any odd characters.


Your line endings are wrong. Shove it through dos2unix or tr -d '\r'.

Tags:

Bash

Expect

Bin