1

I would like to have a function (or may be some kind of executable) that can be called via Terminal from wherever I am (from any directory) and this function does a simple job of converting between units, e.g. from meter to feet. Is such task realizable? I can code in Fortran90 and C, the former being more familiar with than the latter. I work on MacOS X platform.

For the sake of simplicity let's say that my function written in C takes a string and print it in standard output:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
    char str[4];
    strcpy(str, argv[1]);
    printf("input = %s \n",str);       
}

Then compiling it to generate an executable named "example". I would like to be able to call this program globally, so that by executing $example abc it will print input = abc in the terminal window. I have tried placing this executable under the same directory as the gcc's (gfortran, gcc, g++, etc) since this path has been set to the environment variable but it didn't work. I can't call it from outside this directory.

nougako
  • 113

1 Answers1

0

To call it from anywhere in your system, your application must be in a directory that is part of the variable PATH.

On terminal issue:

echo "$PATH"

or

printf "%s\n" $PATH

and you will see something like this:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

That is the list of locations where the system will search for the application.

Move you compiled application to any of those directories: usr/local/bin for example, enable the executable bit (chmod +x) and that's all.

jcbermu
  • 17,822