* Re: How to make emacs to run my ~/.bash_profile
[not found] ` <mailman.7050.1253360978.2239.help-gnu-emacs@gnu.org>
@ 2009-09-20 3:55 ` Tim X
2009-09-20 9:45 ` Pascal J. Bourguignon
0 siblings, 1 reply; 4+ messages in thread
From: Tim X @ 2009-09-20 3:55 UTC (permalink / raw)
To: help-gnu-emacs
Wang Lei <fxhjgzydw@gmail.com> writes:
> I think .bash_profile is only executed once, just after login.
> .bashrc is the actual executed file when starting a shell.
Just to try and clarify....
The .bash_profile is only executed for a login shell.
The .bashrc is executed for every shell.
The idea is to put all the static environment settings in your
.bash_profile and dynamic settings that change depending on dynamic data
in the .bashrc. At a guess, I'd say this seperation was from the days
when computers were a lot slower and you worked hard to minimise the
work being done. As sub-shells inherit the environment of their parent
shell, there is no advantage in redefining static data every time a new
shell is executed. On the other hand, sometimes you have stuff you do
want executed every time a new shell is run (remember, all commands are
executed in their own shell - its not just 'terminals') and sometimes
you have something dynamic you want updated before each command
executes. As an example, in the old days, most shells had no facility to
set a dynamic prompt that changed based on the directory you are in. At
this time, you often created a dynamic prompt that showed things like
directorys by adding a line to your .bashrc (or shrc as bash didn't
exist yet) that would reset the prompt when you changed directories).
The trick to have emacs shell mode know about everything in your
.bash_profile is to ensure that file is executed /before/ emacs is
started.
This can sometimes be a little tricky, depending on how you start
emacs.
Many people start emacs from a menu/icon provided by their window
manager or they start emacs from within a terminal. However, most
distributions don't setup X in such a way that your .bash_profile is
executed before starting your window manager.
The idea with the .bash_profile is that you run it in an early user
process so that all the sub-processes you execute from that point
onwards inherit the environment settings created by your
.bash_profile.In essence, you end up with a type of process tree. At the
root of the tree you have the process that is executed by
xsession. Normally, this process will start your window manager using
'exec' so that the window manager takes over the process that calls it
(rather than forking another process). When you exit your window
manager, it exits that process and terminates your X session. If you
ensure your .bash_rpfile is executed before the window manager, then
your window manger process will inherit all your .bash_profile settings
and consequently, any new processes executed by your window manager will
also inherit that data.
One way to ensure all the processes you start as a user have your
.bash_profile envrionment is to ensure that the xsession that starts
your window manager sources .bash_profile. How you do this depends on
your configuration.
If your running a failrly standard setup and have not created your own
.Xsession script, you can
1. Edit the system Xsession script so that it runs in a login shell (by
adding arguments to the /usr/bin/sh or /usr/bin/bash line at the
top. This can be tricky to work out as there are normally a number of
scripts run as the Xsession startup process, but most are sourced rather
than executed. If the script is sourced, modifying any 'hash bang' line
at the top will have no effect. You have to find the script that is
executed rather than just sourced.
2. Edit the Xsession script that calls your window manager and add a
line to source your .bash_profile
The problem with both these solutions is that under most distros, those
files are managed by the native package management system. Everytime you
get an update of those packages, either your mods are lost or updated
config files (often with bug fixes) don't get installed.
If you have your own custom .Xsession script, you can just add a line
to source your .bash_profile script.
If all of these options make you uncomfortable or you don't have the
confidence to go hacking around your dsitros config files, there are
simpler alternatives, but they also create less consistency in your
environment.
1. Start emacs from a terminal, but ensure you have sourced your
.bash_profile before starting emacs. This will ensure the emacs process
inherits your env settings and consequently, any sub-processes emacs
starts (such as a shell) will also inherit those values. If your window
manager starts your terminal with a menu item, icon or hot key, you can
often modify the command it calls to make the terminal a login
terminal. For example, if you had a menu/icon/hot key that executed the
command
/usr/bin/xterm
you could modify it to run as a login terminal by adding the -ls switch
/usr/bin/xterm -ls
(I /think/ gnome-terminal might have an option in its profiles to do
this).
Another alternative is to customize emacs. If you type
M-x customize-group <ret> shell <ret>
you will find an option called 'Explicit bash args', where you can set
the arguments past to bash when creating a shell. You could add the
-login argument to make the shell a login shell.
The weakness witht he above two approaches is that they don't provide a
consistent envrionment. Other commands you execute from your window
manager menus/icons/hot keys will not have your .bash_profile settings,
only commands executed from the login terminal (in the first case) or
from emacs (in the second case) will know about these settings.
Note also that there is also the /etc/environment file, which can be
used to set environment values that are inherited by /all/
processes. Some environment information that is common to all processes
can be set in this file. However, you should not set values that are
specific to an individual user because the setting in this file are set
for /all/ processes, inclding root processes and processes run by other
users.
>HTH
Tim
> On 9/19/09, hap 497 <hap497@gmail.com> wrote:
>> Hi,
>>
>> I often start a shell in emacs using 'M-shell'. But what i find out it
>> emacs does not execute my ~/.bash_profile.
>> Therefore my alias does not work in emacs shell.
>>
>> Can you please tell me how can I make emacs run my ~/.bash_profile
>> whenever I do a 'M-shell'?
>>
>> Thank you.
>>
>>
>>
--
tcross (at) rapttech dot com dot au
^ permalink raw reply [flat|nested] 4+ messages in thread