Global executables

14 views
Skip to first unread message

sanket

unread,
May 26, 2014, 10:00:09 PM5/26/14
to wncc...@googlegroups.com


Hi,
For calling a particular executable I have to give the complete path for calling in shell.I would like to call that executable from every place without specifying a relative path to executable .(for ex g++). I would to create such a executable or run a script simply by typing it's name.
Can any one help me on this?

Global executables(I just made up the name)

Sanket Kanjalkar
CSE IITB 2nd year

Chandramouleshwar S

unread,
May 27, 2014, 9:35:26 AM5/27/14
to wncc...@googlegroups.com
I think you can copy your executable to /usr/bin directory to make it global executable


--
--
The website for the club is http://wncc-iitb.org/
To post to this group, send email to wncc...@googlegroups.com
---
You received this message because you are subscribed to the Google Groups "Web and Coding Club IIT Bombay" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wncc_iitb+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Pritam Baral

unread,
May 27, 2014, 9:39:39 AM5/27/14
to wncc...@googlegroups.com
Look up the very simple concept of PATH (environment variable, not the movie).

@Chandramouleshwar: DO NOT DO THAT!

There is a reason we have packages on Linux. Unlike Windows where applications write stuff willy-nilly on the filesystem. PATH even has a separate section that packages do not touch, since they are intended to be used manually: /usr/local/{bin,share,lib,etc}.

Heck, if you think copying to system paths (/usr/local is a system path because it is available to every user on the system) is too much and you want a command available only to you, create a bin directory in your $HOME folder. Most user-friendly distros automatically add it to the PATH on login. If not, you can always manually add it to the PATH in your ~/.profile.

 

Regards,
Chhatoi Pritam Baral



On Tue, May 27, 2014 at 7:30 AM, sanket <sanke...@gmail.com> wrote:

--

Kausik Subramanian

unread,
May 27, 2014, 10:28:36 AM5/27/14
to wncc...@googlegroups.com
Copy the executable/script to /usr/bin 

​sudo cp <executable> /usr/bin



On Tue, May 27, 2014 at 7:30 AM, sanket <sanke...@gmail.com> wrote:

--
--
The website for the club is http://wncc-iitb.org/
To post to this group, send email to wncc...@googlegroups.com
---
You received this message because you are subscribed to the Google Groups "Web and Coding Club IIT Bombay" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wncc_iitb+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Kausik Subramanian
IIT Bombay | CSE | 3rd Year | Internship Coordinator | SIlverscreen Convenor


Shyam JVS

unread,
May 27, 2014, 10:28:38 AM5/27/14
to wncc...@googlegroups.com
I think you could simply add the following lines in the .bashrc file (present in your home directory) to do this:

# My list of aliases
shyam(){
        var=$(pwd)
        cd  'path to the executable's directory'
        ./a.out
        cd $var
}
alias run=shyam

PS: Do not forget to close and restart your terminal to make the alias 'run' functional.

This combination of a function + an alias would help you save the pain of copying over your executable to
a different location !

Regards,
Shyam

Chandramouleshwar S

unread,
May 27, 2014, 10:36:53 AM5/27/14
to wncc...@googlegroups.com
As said by Pritam its not good to add the file to /usr/bin.Instead you can use /usr/local/bin or u can create a bin folder in your home directory and add the path to that directory to the environment variable PATH.


--

Manish Goregaokar

unread,
May 27, 2014, 12:36:26 PM5/27/14
to wncc...@googlegroups.com
@Shyam best not to cd around, especially when you want to call an executable. What if I wish to give a relative path to the executable as an argument?

Cleaner would be 

shyam() {
/full/path/to/a.out
}

even more cleaner, 

alias shyam=/full/path/to/a.out

when you say `./a.out` the dot-slash isn't a command "execute". It just means "a.out from the local dir", and the terminal executes as usual. The reason just `a.out` doesn't work is because that does a search on $PATH only -- it's sort of for security to avoid mistypes. But if you give a full path, that's unambiguous, so there's no issue. dot-slash is not necessary to execute.

Nor is moving necessary. A symlink (`ln -s`) to the desired location should do the job if you don't want to move. The best solution is to create ~/bin (or something), and `ln`/`cp` to that.



-Manish Goregaokar

Shyam JVS

unread,
May 27, 2014, 1:25:39 PM5/27/14
to wncc...@googlegroups.com
Yup Manish..
I've tried the alias shyam=..... !
It doesn't work. It says such a file or directory doesn't exist.
This is because bash looks for that path wrt the current path or wrt the standard $PATH.
The same is the case even with the shyam(){....} option.
That is what essentially made me do this whole 'cd' thing.

Pratyush Nalam

unread,
May 27, 2014, 1:25:39 PM5/27/14
to wncc...@googlegroups.com

This is exactly what you shouldn’t do

 

Pratyush Nalam

http://www.cse.iitb.ac.in/~pratnala

Sourabh Bhat

unread,
May 27, 2014, 3:23:58 PM5/27/14
to wncc...@googlegroups.com
The PATH environment variable is designed for exactly this purpose.
You may simply add the following line in "~/.bashrc" file, if your program is located at /directory/of/program/yourProgName

export PATH=/directory/of/program/:$PATH

How does this work?
When you start the terminal, ~/.bashrc file is loaded and all commands in the file are run. The above export command modifies the PATH environment variable, to add your program path, such that this directory is searched when you type any command at the terminal.

Now you can run at terminal:
yourProgName

It is similar to set or setx commands in windows.

- Sourabh

Best Regards,
-- Sourabh

Pritam Baral

unread,
May 27, 2014, 3:57:34 PM5/27/14
to wncc...@googlegroups.com
Adding that line to ~/.bashrc is problematic, ~/.profile is better. Here's why:

~/.bashrc gets executed everytime bash runs. You open one terminal: that directory gets added to the PATH. You run bash inside that terminal: that directory gets added again. How DO you run bash inside a shell? shell scripts. Your system already ships with shell scripts; chances are, your graphical desktop environment launches a few at login too.

~/.profile, on the other hand, is sourced only once per (login) session. Perfect!

 

Regards,
Chhatoi Pritam Baral

Manish Goregaokar

unread,
May 28, 2014, 1:28:16 AM5/28/14
to wncc...@googlegroups.com
@Shyam full path, I said. Without the full path it wont work.

-Manish Goregaokar


--

Shyam JVS

unread,
May 28, 2014, 12:32:03 PM5/28/14
to wncc...@googlegroups.com
@Manish Yup, it was full path only that I've used. Doesn't seem to work.

Manish Goregaokar

unread,
May 28, 2014, 12:52:34 PM5/28/14
to wncc...@googlegroups.com
@Shyam got the correct chmod flags set? And is the full path preceded by a slash? What's the error? This always works if the program is an executable.

-Manish Goregaokar


On Wed, May 28, 2014 at 7:57 PM, Shyam JVS <shyam12...@gmail.com> wrote:
@Manish Yup, it was full path only that I've used. Doesn't seem to work.

--

Ajinkya Bapat

unread,
May 29, 2014, 12:14:57 PM5/29/14
to wncc...@googlegroups.com
Shyam, if you are using an alias, you shouldn't prefix the path with a dot. That is probably why you are getting file not found.
"." in *nix systems denotes the current directory.

Shyam JVS

unread,
May 29, 2014, 12:14:57 PM5/29/14
to wncc...@googlegroups.com
Did all that. I don't know why it still cannot find such a file.

Sanket Kanjalkar

unread,
May 29, 2014, 3:45:33 PM5/29/14
to wncc...@googlegroups.com
well my problem is solved , thanks to all

Shyam JVS

unread,
May 30, 2014, 6:01:19 AM5/30/14
to wncc...@googlegroups.com
@Sanket, So which method did you use finally ? :D
Reply all
Reply to author
Forward
0 new messages