1

Some programs can be executed from anywhere with a single one-word command. An example of this is youtube-dl, which is a python program that can be executed with the simple command youtube-dl [input]. As far as I have understood, this is simply because there exists a file called /usr/bin/youtube-dl, and /usr/bin is in PATH. However, I do not understand what I have to do to make something like this myself. Right now, I have a python project called testproject that includes a python program like this: ~/testproject/files/myownprogram.py

What do I have to do to make this a binary executable such as youtube-dl?

I know I can make an alias mop="python ~/testproject/files/myownprogram.py", and this is also what I have done, and it works fine. That is, I can write $ mop, and successfully run my program. But for curiosity's, and learning's, sake, I want to know how to make a file such as the /usr/bin/youtube-dl file, removing the need for aliases.

I find this hard to find information about in search engines... Any help is greatly appreciated! :-)

Edit:

My question differs from the one marked as duplicate, in that I'm not looking to execute it as a .sh-script. I simply want to execute it as a suffix-less one-word command, similar to all the other executables that are in /usr/bin. :-)

Ex.: $ myown should run my program, without the need for aliases or writing ".sh" or ".py" at the prompt. That is, I want to have a file /usr/bin/myown that somehow runs my testproject at the simple command myownfrom anywhere.

iLoveNachos
  • 25
  • 1
  • 6
  • Possible duplicate of [Shell Script: Execute a python program from within a shell script](http://stackoverflow.com/questions/4377109/shell-script-execute-a-python-program-from-within-a-shell-script) – idjaw Oct 04 '15 at 03:05
  • As you've said... You need to put your script in a folder that is in `$PATH`. – Reut Sharabani Oct 04 '15 at 03:07
  • So I have to add a new folder to $PATH for every single project? Wouldn't that make my $PATH very long and ...impractical after a few projects? I have a lot of binaries in the `/usr/bin` folder that can be executed. Can I somehow place my project there, and thus avoid adding new folders to $PATH? – iLoveNachos Oct 04 '15 at 03:14

1 Answers1

0

The applications are being executed "from anywhere" because the system has a specific hierarchy of places it looks for these files (the current directory, then the system path). So, it knows to look in /usr/bin because that's in your system path.

As to ensuring it to use python when it's executed, you should add the following to the top of the file (check out some python application source code and you'll see this):

#!/usr/bin/env python

This tells the system to execute the script using the systems "python" command.

Rejected
  • 4,445
  • 2
  • 25
  • 42
  • Thanks for replying, and thanks for confirming my thoughts about the PATH thing. My file is already executable, and as mentioned works fine when I run either the full path or the alias I created. However, (to my knowledge) I can't run a short command without using aliases. Very specifically, I wonder how youtube-dl went about creating the suffix-less file in `/usr/bin` that made this possible. How do I do the same? – iLoveNachos Oct 04 '15 at 03:11
  • 1
    Unix doesn't care about filenames or extensions; it cares whether the file is marked as executable, and, in the case of scripts, that the proper shebang (#!) line is present. If both of those are there, you can call the file anything you want and it will run. This is why youtube-dl runs, even though it's a Python script. – Joe McMahon Oct 04 '15 at 05:17