1

I hope this question is okay. Basically what I want to do is create a small program that, using the Ubuntu terminal, I could just call it and execute a command from any directory. For an easy example, something like print Hello World would simply print Hello World back out. Any advice on how I could do this?

#include <iostream>

int main(int argc, char* argv[])
{
    for (char letter : arv) {
        std::cout << letter;
    }
}

I think something like that would work, just a really simple program, but how could I get it to install a command that I could use anywhere?
I hope this makes sense, if I should elaborate on something, please let me know.
To explain why this is not a duplicate, I will elaborate a bit to say that I'm not asking how to call a command from the program. It has been partially answered that I can move my executable file to the /usr/bin/ directory, which was helpful, however I am more-so curious on how I can do it so that if a user downloads and uses my program, they won't have to move the file to /usr/bin/, they can just download and have their global command.

Dylan
  • 188
  • 3
  • 12
  • possible duplicate of [How do I execute external program within C code in linux with arguments?](http://stackoverflow.com/questions/5237482/how-do-i-execute-external-program-within-c-code-in-linux-with-arguments) – Eli Algranti Jul 13 '15 at 00:14
  • I think you misunderstood me, I don't want to call the command from the program, I want to call it from the terminal's command line. Like you would call ls or gedit or something. – Dylan Jul 13 '15 at 00:16
  • 4
    Then just put your executable somewhere in the path (/usr/bin, /usr/local/bin) or modify your user's path environment to include some directory inside your home directory. – Eli Algranti Jul 13 '15 at 00:20
  • Okay thanks, I get that part now, but how could I do it "automagically" so that if a user were to use my program they wouldn't need to do anything? – Dylan Jul 13 '15 at 00:26

2 Answers2

4

A command is just a program in one of the directories specified by the environment variable PATH.

Run echo $PATH to see the directories (separated by colons).

A user can "install" your program simply by copying it to any of those directories. Conventionally, /usr/local/bin is for programs installed outside of the package manager, but any one would work. They could also copy it to a new directory and add that directory to their PATH.

If you want it to happen "automatically", then you need to get the user to run a different program that does it for them.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • Thank you very much, this is what I was looking for. If you don't mind me asking you to continue, how would someone add a directory to their `PATH`? I know how to do it in Windows, but I'm not so sure with Linux/Ubuntu. Thanks! – Dylan Jul 13 '15 at 00:37
  • 1
    “A command is just a program in one of the directories specified by the environment variable PATH.” And has the executable bit set: if it does not have that bit set (with `chmod +x command-to-be-given-execute-permissions`), it doesn't matter if it is in the `PATH`. – juandesant Jul 16 '15 at 18:38
-1

You need ti use alias command http://www.hostingadvice.com/how-to/set-command-aliases-linuxubuntudebian/ or move your script/bin to /bin/

smith_15
  • 11
  • 2
  • How would I do this more user-intuitively? Like how when you install maven, you can call `mvn`. I would like it so if a user were to use my program, they would be able to just call a simple command to use it. Such as `print` in the example. – Dylan Jul 13 '15 at 00:20
  • /bin is for core system executables. Not even package managers usually touch that. – Petr Skocik Jul 13 '15 at 00:34
  • @PSkocik Is `/usr/bin` or `/usr/local/bin` preferred? – Dylan Jul 13 '15 at 00:38
  • `/usr/local/bin` is (for executables you want to share with other users, otherwise just put it somewhere under ~, e.g., `~/.bin.` or `~/.local/bin/` and make sure you have that in your $PATH). `/usr/bin` if for packaged programs. – Petr Skocik Jul 13 '15 at 00:45