I want to create a command that runs an executable created by compiling a c
program. I couldn't find a proper solution. Let's say I have a file named myprogram.c
and compile it and have myprogram
as . I want to type myprogram
in any folder in my system and run it. How can I achieve this?

- 83
- 8
-
Does it work if you type `./myprogram` into the command line? – Dylan Kirkby Dec 31 '15 at 00:22
-
Yes, but I don't want to type `./`. I want to be able to run it from any folder, just like `ls` or `pwd`. – user3616495 Dec 31 '15 at 00:23
-
Add the directory `.` to the end of your `PATH` environment variable so it looks like `
:.` – Dylan Kirkby Dec 31 '15 at 00:25 -
@DylanKirkby - or, perhaps, *don't* do that, and save yourself confusion and risk. Sometimes the best answer is "that's a really bad idea, don't do that, find another way." If you can suggest that other way, perhaps you get upvoted. :) – ghoti Dec 31 '15 at 00:39
3 Answers
First find out what your PATH is
echo $PATH
For you this outputs
/sbin:/usr/sbin:/bin:/usr/bin:/usr/pkg/sbin:/usr/pkg/bin/usr/X11R7/bin:usr/X11R6/bin:/usr/local/sbin:/usr/local/bin
Then assuming your program is in the /usr/myprog
directory, append /usr/myprog
to your PATH
(don't forget to separate directories with a colon :
)
export PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/pkg/sbin:/usr/pkg/bin/usr/X11R7/bin:usr/X11R6/bin:/usr/local/sbin:/usr/local/bin:/usr/myprog
Doing this tells the system when you don't specify an absolute path (like ./myprogram) to look in all the directories in PATH
. It is good to add the absolute path of your executable to PATH because adding .
to your PATH
is frowned upon by some (see this question).

- 1
- 1

- 1,427
- 11
- 19
-
Minix is a bit weird. My path is `/sbin:/usr/sbin:/bin:/usr/bin:/usr/pkg/sbin:/usr/pkg/bin/usr/X11R7/bin:usr/X11R6/bin:/usr/local/sbin:/usr/local/bin` which is hard to understand. Also I assume in Minix the .bashrc file is .shrc but I am not so sure about it. By the way my program is under the `/usr/myprog` folder. – user3616495 Dec 31 '15 at 00:38
-
So change it to `/sbin:/usr/sbin:/bin:/usr/bin:/usr/pkg/sbin:/usr/pkg/bin/usr/X11R7/bin:usr/X11R6/bin:/usr/local/sbin:/usr/local/bin:/usr/myprog` – Dylan Kirkby Dec 31 '15 at 00:39
-
`PATH` is simple enough to understand, it is just a set of unix directories separated by colons. Yours includes `/sbin`, `/usr/local/bin` and a few others. – Dylan Kirkby Dec 31 '15 at 00:40
-
Well I guess it was simple enough. Working now. Also, the `.bashrc` equivalent of Minix is under the /root folder named `.shrc` in case anyone needs it in future. Thanks. – user3616495 Dec 31 '15 at 00:46
You place the executable into a directory that your shell already searches for programs, or you add your program's location to that list.
the $PATH
environment variable contains this information. You can add myProgram
's location to it, i.e. export PATH=$PATH:/new/dir
, or just print out $PATH
and copy myProgram
into one of the paths listed there already.

- 1,267
- 2
- 12
- 26