Friday
Dec302011
Problem with #!/usr/bin/env bash
Friday, December 30, 2011 at 3:13PM A few months ago, I created a bunch of Linux shell scripts, and they all gave me an error message. The first line of a script, for example foo.sh, was
#/usr/bin env bash
When I ran this script with "./foo.sh", I got this error message
./foo.sh: line 1: #!/usr/bin/env: No such file or directory
This drove me nuts for a while, and I finally figured out the problem. Although the line looked just fine in vi, I ran
head -1 foo.sh | hd
And got
00000000 ef bb bf 23 21 2f 75 73 72 2f 62 69 6e 2f 65 6e |…#!/usr/bin/en|
00000010 76 20 62 61 73 68 0a |v bash.|
00000017
It turns out that those first three bytes are a UTF-8 Byte Order Mark (BOM), which characterizes the character set that follows. Although vi hides this, you can remove the bytes with ":set nobomb". I also now see that TextWrangler shows "Unicode (UTF-8, with BOM)" at the bottom, and you can click to change it to "Unicode (UTF-8)". I must have somehow used some text editor to create the first shell script with the BOM, and when I copied it to create the other ones, the three bytes propagated like a virus.