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.

4
  • possible duplicate of How do I execute external program within C code in linux with arguments? Commented Jul 13, 2015 at 0: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. Commented Jul 13, 2015 at 0: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. Commented Jul 13, 2015 at 0: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? Commented Jul 13, 2015 at 0:26

2 Answers 2

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.

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

2 Comments

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!
“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.
-1

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

4 Comments

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.
/bin is for core system executables. Not even package managers usually touch that.
@PSkocik Is /usr/bin or /usr/local/bin preferred?
/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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.