Reputation: 123
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 ?
Upvotes: 3
Views: 5447
Reputation: 19
Or You could use the "alias" command, pointing to Your "executable".
If You put the "alias" in Your ".bashrc" file, then Your chosen "alias" will "call Your executable" in all shells.
[.bashrc is the "config file" for bash-shell, it's normally saved in Your home directory]
Making it work as any other "shell command", whithout having to change Your "path" variable.
This is also a simple way to "bind" scripts that You use often, to short simple "shell commands of Your choosing". So that they are "ever present" in Your shell, which I think is very useful.
Upvotes: 0
Reputation: 105
ls runs from any directory because whenever you execute any program file it first looks in your present directory. If it finds it there it will execute it else it will look at you PATH variable and then try to find it in those directories and then execute it. e.g ls is not present in you current dir but it is present in one of directory mentioned in path variable. do, echo $PATH /usr/sbin:/usr/bin:/sbin:/bin:/usr/games The output above shows it will check for "ls" in /usr/sbin,/usr/bin,/sbin,/bin and so on....
Now you can do something interesting,do export PATH= and now execute ls o/p: bash: ls: No such file or directory because ls is not in your present directory and PATH variable is empty. Caution: Save your PATH variable value before you run above command.
Upvotes: 0
Reputation: 1
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).
Upvotes: 3
Reputation: 15633
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.
Upvotes: 1