1

I have few path settings and alias in .bash_profile, and I am exporting those.

For example:

alias gcc=/abc/def/......./myrtgcc
export gcc

And I want use myrtgcc to compile C programs either from terminal or from Eclipse using the command gcc; I expect the system to run myrtgcc somfile.c -o output.

However this is not the case. Even after adding the above alias in .bash_profile, and restarting the system ( or using source .bash_profile ) the changes are not reflected: If I open the terminal and type which gcc, I get /usr/bin/gcc and when eclipse uses "GCC" command it again invokes the same /usr/bin/gcc.

How do I make myrtgcc default across the system, for command gcc

  • Hmmm. I'm not overly experienced with aliases and path priorities, but it sounds like $PATH is taking precedence over the alias $gcc. Instead of an alias, perhaps a simple shell script in ~/bin and placing that location sooner in the $PATH structure would do the trick...? – 0xSheepdog Jun 13 '14 at 16:04
  • Thanks for the response. But, I don't think that may be the reason, because If I explicitly execute > source .bash_profile at terminal and query for > which gcc I get output as /abc/def/......./myrtgcc However, if I open another terminal and query which gcc I get /usr/bin/gcc. – Vijay Rajanna Jun 13 '14 at 16:08
  • Do you have any reason to believe that .bash_profile is ever being sourced automatically? If you are using, e.g., lightdm as your display manager, it does not happen: unix.stackexchange.com/questions/131320/… <- applies to .bash_profile as well. – goldilocks Jun 13 '14 at 16:14
  • You can't export an alias (FWIK); aliases are local to the current shell. You can export environment variables only. – Rmano Jun 13 '14 at 16:51
3

You're getting confused by a few things:

  1. which does not reflect aliases. So when you're trying to check if an alias was made, using which won't help you. Run alias without arguments instead; it'll show all the aliases in effect. Example:

    anthony@Zia:~$ foo
    bash: foo: command not found
    anthony@Zia:~ [$?=127]$ alias foo='echo hi'
    anthony@Zia:~$ foo
    hi
    anthony@Zia:~$ which foo
    anthony@Zia:~ [$?=1]$
    
  2. Aliases affect the shell, not other things. When make, eclipse, etc. attempt to run gcc, shell aliases will not matter. You also can only export variables, not aliases. Here is an example (again with that foo alias) of perl trying to run it:

    anthony@Zia:~$ perl -Mautodie=system -E 'system("foo")'
    "foo" failed to start: "No such file or directory" at (eval 6) line 12.
    at -e line 1
    
  3. Most things that compile things run cc, c++, etc., not gcc. So even if you had overridden gcc, it probably wouldn't have worked.

So, how to accomplish this?

The best approach is to tell make, etc. which compiler to use:

make CC=/path/to/myrtgcc target

You can also pass that in the environment when running /.configure for programs that use autoconf.

The second approach is to create simple shell scripts called gcc, cc, etc. in (for example) ~/bin and put that first in your path. The scripts would look something like this:

#!/bin/sh
exec /path/to/myrtgcc "$@"

and you'd put them first in PATH using something like this in your .bashrc:

PATH="$HOME/bin:$PATH"
export PATH
  • Thanks a lot for this descriptive explanation. This will help. Could you please tell me what is the effect of "$@" ? Does it mean that all the shell scripts in location /path/to/myrtgcc will be executed ? – Vijay Rajanna Jun 13 '14 at 17:18
  • @VijayRajanna "$@" is to pass along the arguments. So if you invoke ~/bin/cc -c file.c -o file.o, the arguments are -c, file.c, -o, file.o, and those will all be passed along to myrtgcc. – derobert Jun 13 '14 at 17:33
0

Read your shell documentation. There are two types of profiles. .bash_profile is executed when you are already logged in and do something such as opening a new terminal. The other type of profile is used when you initially log in to the system and, oddly enough, it is called a login profile. To do what you are wanting to do, try putting your stuff into .profile

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.