2

I have:

#!/usr/bin/env node console.log("It works!"); 

I learned that env finds the node program and interprets it with node. I checked that env exists in /usr/bin.

When I call node itworks.js it works and outputs It works!. However, from what I understand, I should just be able to call itworks.js without node due to the shebang. But when I make this command it says -bash: itworks.js: command not found.

Could someone help me get the shebang to work?

2
  • Make sure the execute bit is set on the file and call with ./itworks.js from within the directory where the file is Commented Jun 4, 2015 at 15:31
  • 1
    FYI, shebangs are not related to bash at all. The OS itself honors the shebang, not the shell, so a shebang works even when you're executing a program through a mechanism that doesn't have any shell -- or any terminal -- involved. (And, in consequence, this question shouldn't be tagged either bash or terminal). Commented Jun 4, 2015 at 15:47

2 Answers 2

5

First of all you need to make the file executable:

chmod +x itworks.js 

Then you need to call it by specifying the path as well. Either:

/where/it/is/on/disk/itworks.js 

or:

./itworks.js 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks...I will accept this answer once it lets me.
0

The reason for :

-bash: itworks.js: command not found 

is because bash looks for programs in directories in the PATH environment variable when you do not say where the file is - it does not look in the current directory unless you tell it.

You could update the PATH variable with the current directory shortcut ., but that can be a security risk, so most run the program like this:

./itworks.js 

Of course if you put your scripts all in one directory then you could add that to PATH in one of your start-up files. For example, if you had a directory called bin in your home directory that held your scripts:

PATH=$PATH:"$HOME/bin" 

You also need to add the execute permissions to the script:

chmod u+x itworks.js 

The u indicates that we only give permission for the current user to execute this file. If we omit the u then anyone can run it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.