1

I'm new to the Linux environment and I noticed there are many programs of which we can use only the program name to start them in the Linux terminal.

E.g. gedit, vi, firefox

Instead of providing all of the program's path, I would like to run my own programs like this in the terminal by only typing the program name. Programs I like to run are written in Java and Python and script files (.jar, .pyc, .py and .class)

How should I proceed?

5

Let's say you want to execute a file called fun.py located on your Desktop using the command funny. Obviously, you'll need python for that, so the exact bash command would be python ~/Desktop/fun.py.

How to do that without having to change the PATH variable:

First create a file called funny with the following contents:

#! /bin/sh
python ~/Desktop/fun.py

and save it to a folder (let's say you have saved it on your Desktop folder).

If non-existent, create a folder named bin in your home directory. Then execute the following commands from a terminal:

source .profile
chmod +x ~/Desktop/funny
mv ~/Desktop/funny ~/bin

You can now enter the command funny to execute the python script.

1

In order to do this you need to update your execution PATH variable. To see your PATH variable you can type echo $PATH in a terminal These are the places that bash will look for your executable files going from left to right.

To add a directory for bash to search for executables you do 1 of 2 things.

  1. Add the files you want to run into one of the PATH directories.

Or

  1. Add a new directory to your path

For option 1.

If your PATH=/usr/bin You could copy the programs you want to execute to there.

For option 2

if you made a new directory with all your programs in /home/bob/bin You could you could add this to your PATH by editing the .bashrc file at the very end add this line: PATH=$PATH:/home/bob/bin

then to load those .bashrc changes type source .bashrc

0

I think /usr/local/bin is a preferred place for user created scripts, etc which you want to start just using name. Anything you copy there can be started simply by name since that is in $PATH. To verify what your $PATH contents type:

echo $PATH

For running bash, sh, or any scripts make sure you have "allow execution" set for the file:

chmod +x  my_script

Related to to running "python my_code.py" or similar things which may require parameters you can create a tiny script which will start that and locate the script into /usr/local/bin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.