-2

My c program file is Numbers.c

cc- Numbers.c -o output.txt 

can give me results in another file. But how can I modify that command line, therefore, I can add the results of Numbers.c to $PATH?

I tried:

cc Numbers.c >>PATH

But there is no change in $PATH when I check it;

echo $PATH
  • What are you trying to achieve? What `cc-` means in your case? – 0andriy Aug 30 '19 at 20:29
  • 1
    When you compile `Numbers.c`, you almost certainly do not want to create the compiled program as `output.txt`. You probably want to call it `Numbers` or maybe `Numbers.exe`. (And then when you *run* the `Numbers` program, maybe you want to redirect its output to `output.txt`). – Steve Summit Aug 30 '19 at 20:41
  • 1
    `mkdir -p $HOME/bin; cp output.txt $HOME/bin/numbers; export PATH=$PATH:$HOME/bin; cd $HOME; numbers …`. Create a bin directory under your home directory; copy the executable to the directory with a faintly meaningful name (remember, `grep` is faintly meaningful, but is obscure to the uninitiated, hence 'faintly'); then ensure the `$HOME/bin` is on your path variable (you need to edit your `.profile` or equivalent to set `PATH` when you login), and you can then run the program with no full pathname specified. You can add `$HOME/bin` to the front of you path if you prefer (I do). – Jonathan Leffler Aug 31 '19 at 00:17
  • 1
    You should make sure you don't accrete too many repetitions of any given directory in your `PATH` variable — any repetition is too many. See [How to keep from duplicating path variable in `csh`?](https://stackoverflow.com/a/137981/15168) and [How do I manipulate path elements in shell scripts?](https://stackoverflow.com/questions/273909/how-do-i-manipulate-path-elements-in-shell-scripts) – Jonathan Leffler Aug 31 '19 at 00:23

1 Answers1

0

How to add an executable program to PATH environment variable?

Make these steps to add a C program to PATH variable.

  1. First of all make a back up from important PATH Variable with echo $PATH > ~/path.txt
  2. Compile the C source code with gcc Numbers.c -o Numbers
  3. Suppose the executable output file Numbers is place under ~/barname.
  4. Run export PATH="$PATH:~/barname" to append ~/barname folder to PATH variable.
  5. Now you can run the executable file i.e: Numbers from any location on Terminal.

I answer to similar question at Iranian Ubuntu community. and now translate it to English.


Edit

By performing jonathan-leffler notes in comment section, i can change above answer to:

Having many different directories inside PATH environment variable not recommend and have performance issue.

If i want to use specific programs only for myself, create a new directory in current user (me) home folder. mkdir $HOME/bin Then copy Numbers program and all of other programs in to it. for example cp ~/barname/Numbers ~/bin. And finally add only one directory i.e: $HOME/bin to PATH variable by export PATH="$PATH:$HOME/bin".

In a situation when working on a real multi user system, like when work as Sysadmin its better to copy programs to /usr/local/bin and then adding this to PATH by export PATH="$PATH:/usr/local/bin. Now every users can access and run programs.


EsmaeelE
  • 2,331
  • 6
  • 22
  • 31
  • 1
    I think it is better to create a single known location — classically `$HOME/bin` — as the repository for your own programs, and you copy the executable to that central location and set your PATH to execute files from that location. If you expect others to use it, then maybe `/usr/local/bin` is a better location. But adding every directory where you've compiled a program to your PATH becomes untenable (impractical) if you get many commands. (My `$HOME/bin` typically has more than 500 programs of various sorts in it — having 500 directories on `PATH` would be a performance disaster.) – Jonathan Leffler Aug 31 '19 at 00:25