-2

I'm just writing some small C scripts and testing them. Why dose

~ Desktop: gcc test.c -o test ; /Users/xx/Desktop/test ;

work just fine while

~ Desktop: gcc test.c -o test ; test ;

dose not work? Why dose OSX/ZSH need the full path to execute the binary?

wowpatrick
  • 4,199
  • 7
  • 31
  • 40
  • This question is asked very often – please do a little research before asking. See also: http://superuser.com/questions/171909/why-is-needed-to-run-a-runnable-file, http://superuser.com/questions/248512/why-do-i-get-command-not-found-when-the-binary-file-exists?rq=1 and http://superuser.com/questions/541749/why-do-i-need-for-commands-to-run-on-mountain-lion?rq=1 – slhck Jul 06 '13 at 13:38
  • Sorry, my bad! It was a dumb question, If I'd just thought about the call for a minute I could have realised myself that the call fails because it the new executable is not in $PATH. – wowpatrick Oct 28 '13 at 19:14

1 Answers1

1

To be able to run executables from the current directory you need to add . to your PATH environment variable. This can be done by running PATH=$PATH:. within the shell or adding export PATH=$PATH:. to your ~/.zshrc.

The other alternative is to prefix the commands you wish to run with ./ making your command

~ Desktop: gcc test.c -o test ; ./test ;

Mattias
  • 601
  • 6
  • 11
  • 4
    Including `.` in your PATH is generally considered a **very** bad idea. That can lead to commands behaving very differently depending on the directory which you were in when you ran the command. It can also lead to mistakenly running malicious code that is in your current directory (such as a directory which you just unpacked from a download). Getting used to using the leading `./` is a much better way to go. – qqx Jul 06 '13 at 13:05