2

I cannot make a file executable from the command line on a Mac. This is what I'm typing in terminal

cd ./Desktop
echo 'say hello' > hello
chmod +x hello
hello

It runs when I double-click on the file, but I can't run it from the command line. I merely get "command not found".

  • 2
    Try ./hello just in case your $PATH doesn't include the current directory. – steve Oct 4 '15 at 15:49
  • 1
    See: en.wikipedia.org/wiki/Shebang_%28Unix%29 – Cyrus Oct 4 '15 at 15:56
  • say hello is not a shell script by itself. You need to tell the shell which shell should be used to execute it. Run the script using "sh hello" and it should work. Or add the shebang in the script. – Sharninder Oct 4 '15 at 18:03
  • 1
    This is not a Mac thing, what you are seeing is true of all modern Unixes. – ctrl-alt-delor Oct 4 '15 at 21:09
3

There are two mistakes in your commands.

Your first mistake is that your file is not in a correct executable format. The first line of a shell script must start with #!. If it does not start with those characters, it is not a shell script.

Some shells have workarounds for such incorrectly formatted shell scripts. But those workarounds are only in place when the script is started from a shell and not when the script is started from another program. Additionally different shells have different workarounds for incorrectly formatted shell scripts.

All in all that means relying on those workarounds will result in unpredictable behavior, and you should always include the #! line. For example you could have typed:

echo '#!/bin/sh' > hello
echo 'say hello' >> hello

The other mistake is that you did not provide a path to the script when trying to execute it. You should have typed:

./hello

When you only typed hello, the shell will search through the PATH for the command. For security reasons . is not in PATH. If you regularly need to create shell scripts, it is a good idea to create a directory for those shell scripts and put that directory in your PATH. I personally use ~/bin for both shell scripts and occasionally small compiled programs.

  • That's very helpful. Thanks. I had used the shebang in a previous version of the script, and this had failed as well. It seems the key issue was placing the script in my PATH. I had assumed that any script would be executable from the command line wherever it was placed, as long as chmod was run... – Nick Riches Oct 4 '15 at 19:28
  • @NickRiches it is executable where ever placed as long as you chmod +x. The thing is that the current working directory is not in the search path so will not be found, so you have to do ./hello or ./Desktop/hello depending on where the file is. This is all for security reasons (as stated in this answer), what would happen if you wrote a script called ls. should it be run instead of /bin/ls, or should /bin/ls be run. If the latter then what happens when you install a now package that contains /bin/hello your script will then stop working, and something else will happen. – ctrl-alt-delor Oct 4 '15 at 21:08
  • Thanks. That makes complete sense. I'm relatively new to UNIX-based systems. I hope this is useful for people – Nick Riches Oct 6 '15 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.