Start a programs in terminal only by name

15,905

Solution 1

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.

Solution 2

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

Share:
15,905

Related videos on Youtube

dimi
Author by

dimi

Updated on September 18, 2022

Comments

  • dimi
    dimi almost 2 years

    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?

    • Elder Geek
      Elder Geek over 9 years
      Hint: All the programs that run by name in the terminal are on your path. I believe the .jar, .pyc, .py and .class files you mention need to be interpreted to run so if I'm correct, CLI won't work for these
    • heemayl
      heemayl over 9 years
  • dimi
    dimi over 9 years
    is there any thing to update in .profile file
  • JD Schmidt
    JD Schmidt over 9 years
    You optionally can but the .bashrc should work. You can see the differences here: superuser.com/questions/278433/…
  • dimi
    dimi over 9 years
    it worked thank you for explain it very briefly
  • Jacob Vlijm
    Jacob Vlijm over 9 years
    Also an interesting post with a few good answers: askubuntu.com/questions/465109/…