How can i run a linux executable from any directory in terminal?
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).
How to make a programme executable anywhere in the SHELL
- Make the scripts executable:
chmod +x $HOME/scrips/*
This needs to be done only once. - Add the directory containing the scripts to the
PATH
variable:export PATH=$HOME/scrips/:$PATH
(Verify the result withecho $PATH
.) Theexport
command needs to be run in every shell session. For convenience, you want to add that command to the$HOME/.profile
file so it is run automatically for you at log-in time.
Now you can execute script.pl some-arguments
or script.py some-arguments
from anywhere.
How do I run a program with a different working directory from current, from Linux shell?
Call the program like this:
(cd /c; /a/helloworld)
The parentheses cause a sub-shell to be spawned. This sub-shell then changes its working directory to /c
, then executes helloworld
from /a
. After the program exits, the sub-shell terminates, returning you to your prompt of the parent shell, in the directory you started from.
Error handling: To avoid running the program without having changed the directory, e.g. when having misspelled /c
, make the execution of helloworld
conditional:
(cd /c && /a/helloworld)
Reducing memory usage: To avoid having the subshell waste memory while hello world executes, call helloworld
via exec:
(cd /c && exec /a/helloworld)
[Thanks to Josh and Juliano for giving tips on improving this answer!]
How to run a .sh-script from any path in a terminal?
One option is simply to type the path to the script:
~/Desktop/script
This works fine, but gets a bit unwieldy.
This is what the PATH
environment variable is for. And it is what $HOME/bin
is for.
- Create yourself a directory
$HOME/bin
. Put all your executable scripts in it (make them executable withchmod +x script
if need be††). This way, there's one place to look for the scripts you want to run. - Add
$HOME/bin
to yourPATH
. I put mine at the front:PATH="$HOME/bin:$PATH
, but you could put it at the back if you prefer. - Update your
.profile
or.bash_profile
(or possibly.bashrc
) file to setPATH
. Beware of a continually growing PATH, though.
As tripleee noted, once the command is installed in a directory on PATH
, you no longer type ./script
, but just script
. This is exactly like you type ls
and not /bin/ls
, etc. Once the program is installed in a directory on your PATH
, it is (for many purposes) indistinguishable from a system-provided command.
I have about 500 scripts and programs in my $HOME/bin
directory.
Note that this doesn't require any special privileges. If you have administrator access to your machine and you think other users might find your commands useful, then you could install the scripts/programs in one of the system-provided directories on your PATH
. However, it is usually best not to add programs to any of:
/bin
/usr/bin
/sbin
/usr/sbin
There is often/usually /usr/local/bin
which is a suitable place for widely used commands not provided by the system.
†† It would be better to use chmod a+x,go-w script
; your scripts should not be writable by other people. You could even simply use chmod 555 script
or chmod 755 script
. I tend to keep my scripts non-writable. That way, I have to go through a formal change process with the version control system. It means there's less danger of uncontrolled changes.
How can I run any executable file from outside of current directory?
The correct format would be to either start from root, /
, or to use the current directory, .
. These examples assume that "~$" is your prompt.
Using /
from anywhere on your system
~$ /home/My_folder/Current_folder/a.out
Using .
from the /home/My_Folder
directory
~$ ./Current_folder/a.out
How to run commands in any directory in macOS Terminal?
Homebrew usually links the necessary executables to /usr/local/bin
directory, which should be in your $PATH
. Thus, when you execute a command like sdcc
, your shell will seek through the $PATH
directories, and when it looks at /usr/local/bin
, it will find sdcc
, follow the link and execute it.
Some packages do not perform this linking, which means you cannot execute them without knowing where they are. You can ask Homebrew to tell you where a package is installed: brew --prefix <formula>
, where <formula>
is the package name (e.g. brew --prefix sdcc
). The executable files will normally be under a bin
subdirectory. For example, brew --prefix sdcc
would likely tell you something like /usr/local/opt/sdcc
; then you can invoke sdcc
using /usr/local/opt/sdcc/bin/sdcc
, without having to cd
there. You could also put /usr/local/opt/sdcc/bin
into your $PATH
directly, though I do not recommend it. Another alternative is to create your own bin
(mine is in $HOME/.local/bin
), put it in $PATH
, and link there (ln -s <source> $HOME/.local/bin/
) any executables you wish your shell to easily find.
However, with Homebrew packages, I strongly suggest you do not try to imitate Homebrew by yourself, by installing things in Homebrew's domain. You can confuse Homebrew and create problems. Instead, let Homebrew do it.
If you need to install a package on a different OS than the one you are downloading at, you may need to first find out the bottle tag for the target (installation) computer. For example, for Big Sur, it is
big_sur
. Seebrew info --json <formula>
, underbottle.stable.files
you should find the bottle tags. Use--bottle-tag <tag>
in step 1 and 2 to select the right OS.Use
brew fetch --deps <formula>
to download (but not install) a package, including its dependencies. If you use the--verbose
flag, Homebrew will tell you where it downloaded each of the files.If you haven't used
--verbose
and still want to know where the downloaded files are:brew deps <formula>
will list all the packages it depends on. If a package needs to be compiled, you may need to also use the--include-build
option.brew --cache <formula>
will show you where a package file is downloaded.If you need to copy a package file to another computer, you should find out where the Homebrew would expect to find it: use
brew --cache --force-bottle <formula>
on the target computer, and copy the package file there. Don't forget to do that for each dependency package as well.After that,
brew install <formula>
will install from cache.
The only part of this process that needs internet connection is the first step, brew fetch
.
How to execute bash script from any location?
If you want to run the script from everywhere you need to add it to your PATH
. Usually /usr/local/bin
is in the path of every user so this way it should work.
So check if in your system /usr/local/bin
is in your PATH
doing, on your terminal:
echo $PATH
You should see a lot of paths listed (like /bin
, /sbin
etc...). If its not listed you can add it. A even better solution is to keep all your scripts inside a directory, for example in your home
and add it to your path.
To add a directory in your path you can modify your shell
init scripts and add the new directories, for example if you're usin the BASH
shell you can edi your .bashrc
and add the line:
PATH=$PATH:/the_directory_you_want_to_add/:/another_directory/
This will append the new directories to your existing PATH
.
How can I run my script from anywhere(outside the current directory) in my system?
In Unix systems, absolute paths start with / . So you can use absolute path to run a script. In your case all you have to do is run this command from the terminal sh /home/testing/program/test.sh
. It does not matter from which directory you run the command from until you use the absolute path of the script.
Also the file in which the output is written should be defined with absolute path in script file. For example, in your case
/home/centos/rr/email-body.txt
so that the file is recognized by the OS even when the script is executed from the other directory or from any directory whatsoever.
Related Topics
Basic Build Issue Regarding Libs, Pkg-Config and Opencv
How to Make Webdriver Testsuite Created in Windows Machine to Run in a Linux Box
Having Linux Persist Memory Changes to Disk
Gitlab Ce Doesn't Add a Public Key to Authorized_Keys
Arm Linux ":Start_Kernel Is Not Calling After Decompressing UImage"
Get Apache Total CPU Usage in (Linux)
Compiling Kernel Error: Stdio.H: No Such File or Directory
Maximum Number of Threads Allowed to Run
Linux Shared Library Depends on Symbols in Another Shared Library Opened by Dlopen with Rtld_Local
Sublimetext3 Build on The Server Over Ssh
Eclipse-Mars on Linux: Black Background Color in Tooltip's
How to Change Some "Special" Gui Colors in Eclipse
How to Get Jenkins Working with Binaries from a Subfolder of The Root User
How to Enter Private Key Password with Ansible
Cannot Connect to The Docker Daemon at Unix:///Var/Run/Docker.Sock