Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell script saying Command not found. Ignoring shebang?

Tags:

linux

shell

It seems like my shebang is being ignored. When I try to execute a shell script I wrote, I get an error:

% ls -alh /usr/sh
-r-xr-xr-x  1 root  1011   139k Sep 16 01:29 sh
% ls -alh foo.sh
-rwxr-xr-x  1 george george    21B Sep 16 00:59 foo.sh
% cat ./foo.sh
#!/usr/sh
echo "lol"
% ./foo.sh
./foo.sh: Command not found.
% sh ./foo.sh
lol

Yes, it's at /usr/sh. I put it there.

But as you can see it works just fine when I instruct sh to execute it. What gives?

I don't think $PATH is applicable but in some search results trying to solve this problem, people have asked for it:

% echo $PATH
/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/root/bin
like image 471
deed02392 Avatar asked Sep 01 '25 03:09

deed02392


1 Answers

The reason for this can be one of several things. To list the most common:

  1. The file does not have executable permissions. Use chmod a+x foo.sh to give all users execute permissions.
  2. Your shebang is incorrect, double-check the path. Use which sh and copy the result after a #!
  3. Your lines are delimited with DOS feeds. Scripts need to be delimited with \n. Text editors such as nano allow you to convert by choosing Write-out then pressing Alt+D to toggle DOS format off. Utilities such as dos2unix can convert files directly on the command line (although it may need installing first).
like image 191
deed02392 Avatar answered Sep 03 '25 03:09

deed02392