Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i run a linux executable from any directory in terminal?

Tags:

linux

I know that the linux commands like ls, cd and others are executables and can be accessed from any directory. My question is how can i run an executable created by me from any directory in terminal ?

like image 346
Radu Avatar asked Jan 28 '26 09:01

Radu


2 Answers

Read about the PATH variable. It could be set by your shell. Check with echo $PATH its current value. It is also used by several exec(3) functions. BTW, having a long $PATH is bad taste and messy (and could be inefficient).

If your login shell is /bin/bash you could edit your ~/.bashrc (used for interactive shells) to add something like

 PATH="$PATH:/something/more"

but on several recent Linux distributions, the $HOME/bin/ directory is already part of your PATH, and you might add scripts, executables, or symlinks to them in it.

So (when $HOME/bin is mentioned in $PATH) I don't recommend extending your PATH, but rather adding appropriate executables, executable scripts or symlinks into that $HOME/bin/ directory.

Of course, if you have some executable in $HOME/someproject/someprog you can still explicitly run it with a shell command starting with $HOME/someproject/someprog.

Your build procedure might also have some installation step. For example, if you use GNU make as your build automation, you might have an install phony target in your Makefile which copies the executable after its compilation into some appropriate place. See also hier(7) & install(1), and autoconf.

Look for inspiration into the source code of some existing free software, e.g. on github.

Notice that many utilities (e.g. cron and your crontab(5)) don't use your interactive PATH (but some reduced default one). So you might want to give an absolute path of some script when using crontab(1).

like image 62
Basile Starynkevitch Avatar answered Jan 31 '26 22:01

Basile Starynkevitch


The directory that the executable is located in must be available in your $PATH. You change the path by modifying your shell's startup files (typically ~/.bash_profile or ~/.bashrc if you're using bash), adding

PATH="$PATH:/some/directory"

... where /some/directory is the path to the directory where your executables are located.

The change will take effect when you open a new (login) shell.

like image 40
Kusalananda Avatar answered Jan 31 '26 21:01

Kusalananda