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.
Register an .exe so you can run it from any command line in Windows
You need to make sure that the exe is in a folder that's on the PATH
environment variable.
You can do this by either installing it into a folder that's already on the PATH
or by adding your folder to the PATH
.
You can have your installer do this - but you may need to restart the machine to make sure it gets picked up.
running a python script in the command line from any directory
For a simple script, the easiest way to make it executable is to simply add a Python shebang line, save the script to a directory that's on your PATH
(e.g. /usr/local/bin
) and set the executable bit on the script.
E.g.
#!/usr/bin/python3
import sys
print('Hello, world! I am Python', sys.version)
saved as /usr/local/bin/python-hello
followed by chmod u+x /usr/local/bin/python-hello
will let you execute python-hello
from anywhere.
More complex scripts are best made executable by packaging them correctly with a proper console_scripts
entry point -- though something packed with PyInstaller would also work, although it'd be much heavier.
EDIT
A script with multiple modules should be organized into a package, e.g.
python_hello/
__init__.py
__main__.py
greetings.py
__main__.py
could then look like
def main():
# ...
if __name__ == "__main__":
main()
This way you can run the script with python -m python_hello
as well as set up python_hello.__main__:main
as a console script entry point.
Why can't Git Bash run my executable?
To run a program in the current directory in bash, you put ./
in front of it. So in your case:
$ ./sqlite3.exe
When you run sqlite3
, bash will look for a program with exactly that name in all directories of the PATH
environment variable, which by default includes standard locations for executables like /usr/local/bin
but not your current directory. See here for more info on that.
Is it possible to make a Java executable?
After two and a half years I have stumbled upon a more complete answer than was given in 2016. The Java binaries can be embedded within the executable, contrary to John Hascall's answer. This article explains how this can be done in linux and unix like systems by adding a binary payload to a shell script.
I will offer a short summary of the procedure.
Given an executable jar named any_java_executable.jar
Given you want to create an executable named my_executable
Given a script file named basis.sh
with the following contents
#!/bin/sh
MYSELF=`which "$0" 2>/dev/null`
[ $? -gt 0 -a -f "$0" ] && MYSELF="./$0"
java=java
if test -n "$JAVA_HOME"; then
java="$JAVA_HOME/bin/java"
fi
exec "$java" $java_args -jar $MYSELF "$@"
exit 1
The native executable can be created by running the following two commands.
cat basis.sh any_java_executable.jar > my_executable;
chmod +x my_executable;
Then my_executable
is a native executable capable of running the java program without depending on the location of the jar file. It can be executed by running
./my_executable [arg1 [arg2 [arg3...]]]
and can be used anywhere as a CLI tool if placed in /usr/local/bin
.
Running .sh scripts in Git Bash
Let's say you have a script script.sh
. To run it (using Git Bash), you do the following: [a] Add a "sh-bang" line on the first line (e.g. #!/bin/bash
) and then [b]:
# Use ./ (or any valid dir spec):
./script.sh
Note: chmod +x
does nothing to a script's executability on Git Bash. It won't hurt to run it, but it won't accomplish anything either.
Why am I getting IOException : Cannot run program ./shellScript.sh error = 2, No such file or directory in Java?
You should use ProcessBuilder
to launch or one of the overloads of exec
. You need to specify the pathname to the script and pass the same pathname as the current directory to run the script in:
File pwd = new File("/dir1/dir2");
String shell = new File(pwd, "shellScript.sh").toString();
ProcessBuilder pb = new ProcessBuilder(shell);
// No STDERR => merge to STDOUT - or call redirectError(File)
pb.redirectErrorStream(true);
// Set CWD for the script
pb.directory(pwd);
Process p = pb.start();
// Move STDOUT to the output stream (or original code to save as String)
try(var stdo = p.getInputStream()) {
stdo.transferTo(stdout);
}
int rc = p.waitFor();
Git Bash doesn't see my PATH
Got it. As a Windows user, I'm used to type executable names without extensions. In my case, I wanted to execute a file called cup.bat
. In a Windows shell, typing cup
would be enough. Bash doesn't work this way, it wants the full name. Typing cup.bat
solved the problem. (I wasn't able to run the file though, since apparently bash couldn't understand its contents)
One more reason to switch to posh-git..
Thanks @Tom for pointing me to the right direction.
How to get a python script to run as an executable?
Add this to the top of your script
#!/usr/bin/env python
or
#!/usr/bin/env python3
depending if you want python2 or python 3
Related Topics
Why Doesn't Set -E Cause a Failure with 'False || False && True'
Ada Program Works in Linux But Not in Gps Windows 10
R Programming - Submitting Jobs on a Multiple Node Linux Cluster Using Pbs
Linux Kernel: Copy_From_User - Struct with Pointers
How to Install Rpy2 via Conda Using Default R Installation
"Bad Interpreter" Error Message When Trying to Run Awk Executable
Combine Two CSV Files Based on Common Column Using Awk or Sed
One-Liner: Print All Lines Except the Last 3
Linux Perf Record: Difference Between Count (-C) and Frequency (-F) Options
Bash Script Properties File Using '.' in Variable Name
How to Make Linux Power Off When Halt Is Run
Why Do I Get /Etc/Cups Conflicts Between Attempted Installs in Yocto
Asp Net Core Linux Err_Connection_Refused
Python Error "Attributeerror: 'Module' Object Has No Attribute 'Sha1'"
Output Stdout and Stderr to File and Screen and Stderr to File in a Limited Environment