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?

Fabby
  • 34,259
dimi
  • 35

3 Answers3

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.

codingEnthusiast
  • 754
  • 6
  • 16
  • it worked thank you for explain it very briefly – dimi Feb 03 '15 at 20:18
  • 1
    Also an interesting post with a few good answers: http://askubuntu.com/questions/465109/where-should-i-put-my-script-so-that-i-can-run-it-by-a-direct-command/465162#465162 – Jacob Vlijm Feb 04 '15 at 08:34
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

  • is there any thing to update in .profile file – dimi Feb 03 '15 at 19:58
  • You optionally can but the .bashrc should work. You can see the differences here: http://superuser.com/questions/278433/whats-the-difference-between-profile-and-bash-profile-and-when-do-you-config – JD Schmidt Feb 03 '15 at 20:02
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.

ajaaskel
  • 103