1

I wrote a programm in the c language, that I use all over the place, but every time I want to use it, I have to type out the whole path. Can I add my script to the bash terminal?

1

You need to store your program in one of the directories searched for commands (also referred to as the command path).

If you enter echo $PATH those directories will be listed.

I suggest you copy your executable to /usr/local/bin

sudo cp your-program /usr/local/bin

Make it executable (if you haven't already)

sudo chmod /usr/local/bin/your-program

You can then just type your-program to run your program.

2

As per other answers, $PATH is the environment variable containing paths in which the shell looks for commands (see echo $PATH). You want to either:

  • Put the command or a link (see man ln and particularly the -s switch) into an element of path. The standard place for this is /usr/local/bin, because:

    1. It usually takes precedence, i.e., is checked first, so you can override system commands this way.

    2. You won't have any conflicts with distro packages because they purposefully never install there. You could have conflicts with source built packages (i.e., ones you build yourself) because they usually install there.

    Using a (symbolic) link looks something like this:

    ln -s /path/to/my/command /usr/bin/whatever-you-want-to-call-it
    

    Note the names do not have to match in any way.

  • Add the location of the command to $PATH. This allows you to have directories that are only used by particular users; a typical example is $HOME/bin, which may or may not be in $PATH already. I'm using it as an example assuming it isn't. First you create a ~/bin directory, put your command or a link into it, and now the tricky part: Adding to $PATH. This is best done per user, which is why I've described it that way, although it can be done system wide -- which I recommend avoiding for most people.

    The normal, old fashioned way to do this was adding a line like:

     PATH=$HOME/bin:$PATH
    

    to ~/.profile, because traditionally that is sourced by login shells. However, if you use a GUI login, particularly on Raspbian, you never use a login shell, so that file is never read. Some people circumvent this by adding it to ~/.bashrc, which is read by every shell invocation. This is sort of a messy but probably not dangerous solution, since it may result in a $PATH which is duplicated many times. For some solutions to that problem, see this U&L Q&A.

There are also various solutions to the lightdm issue (Raspbian's GUI login) scattered around, such as this one.

2

Move your program to ~/bin.

Raspbian (and Debian based distros) automatically add $HOME/bin to the PATH if that directory is present. You can check this in ~/.profile:

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

Therefore, you can put your C programs in ~/bin, and they'll work from anywhere while you're logged as that user. If you want other users to access it, use the system wide PATH for user programs: /usr/local/bin

Sources:

1

Bash has a built in called "alias" to make you own bash commands. See "man alias"

0

Programs developed in Linux are normally added to /usr/local/bin while software included in the distro is put in /usr/bin or /usr/sbin. The difference is sbin normally requires root privileges.

Type $PATH at the terminal to see your system path and you'll see /usr/local/bin there showing it's one of the places the shell will look when you type in a program name. When multiple users are developing on the same system it's not uncommon to add a /home/username/bin to their own path. I normally add my scripts folder to my own path to have them quick at hand.

For example to add my own scripts directory to my path it would be PATH=$PATH:$HOME/scriptsif you want everyone to have access then put it in /usr/local/bin with the appropriate permissions.

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.