Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

use eval in .profile

354 views
Skip to first unread message

Hongyi Zhao

unread,
Dec 16, 2019, 8:01:25 AM12/16/19
to
Hi,

I installed pyenv and pipenv for python environment manage. And based on
the corresponding instructions, I add the following code into

~/.profile


eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
eval "$(pipenv --completion)"

All of the avove codes will add the corresponding command completions.

But, I found this will don't tacke effect.

OTOH, if I put the above code into ~/.bashrc,
it will do the trick.

So, I want to know whether should I put the eval command into .profile
for this purpose?

Regards




Chris Elvidge

unread,
Dec 16, 2019, 8:24:03 AM12/16/19
to
I'd suggest you did this:
# for i in .bash_profile .profile .bashrc; do [ -f "$i" ] && echo "touch
~/ran$i" >> "$i"; done
(obviously all one line)
and then relogin and check your home directory for "ran*" files.
You can then see whether .profile or .bashrc (or indeed .bash_profile)
is the correct place for your code.


--

Chris Elvidge, England

Hongyi Zhao

unread,
Dec 16, 2019, 9:26:56 AM12/16/19
to
On Mon, 16 Dec 2019 13:23:57 +0000, Chris Elvidge wrote:

> I'd suggest you did this:
> # for i in .bash_profile .profile .bashrc; do [ -f "$i" ] && echo "touch
> ~/ran$i" >> "$i"; done (obviously all one line)
> and then relogin and check your home directory for "ran*" files.
> You can then see whether .profile or .bashrc (or indeed .bash_profile)
> is the correct place for your code.

I've considered all of these cases:

$ grep -Ev '^(#|$)' ~/.bash_profile
[[ -f ~/.profile ]] && . ~/.profile
$ grep -Ev '^(#|$)' ~/.bash_login
[[ -f ~/.profile ]] && . ~/.profile

Regards

Hongyi Zhao

unread,
Dec 16, 2019, 9:41:42 AM12/16/19
to
On Mon, 16 Dec 2019 13:01:22 +0000, Hongyi Zhao wrote:

> eval "$(pyenv init -)"
> eval "$(pyenv virtualenv-init -)"
> eval "$(pipenv --completion)"

I've done the test on the above commands in the (), and found the
following results:

Each of them will expanded into some very complex compound commands/
functions as follows:


-------------- begin from here --------------
$ pyenv init -
export PATH="/home/werner/.pyenv/shims:${PATH}"
export PYENV_SHELL=bash
source '/home/werner/.pyenv/libexec/../completions/pyenv.bash'
command pyenv rehash 2>/dev/null
pyenv() {
local command
command="${1:-}"
if [ "$#" -gt 0 ]; then
shift
fi

case "$command" in
activate|deactivate|rehash|shell)
eval "$(pyenv "sh-$command" "$@")";;
*)
command pyenv "$command" "$@";;
esac
}


$ pyenv virtualenv-init -
export PATH="/home/werner/.pyenv/plugins/pyenv-virtualenv/shims:${PATH}";
export PYENV_VIRTUALENV_INIT=1;
_pyenv_virtualenv_hook() {
local ret=$?
if [ -n "$VIRTUAL_ENV" ]; then
eval "$(pyenv sh-activate --quiet || pyenv sh-deactivate --quiet ||
true)" || true
else
eval "$(pyenv sh-activate --quiet || true)" || true
fi
return $ret
};
if ! [[ "$PROMPT_COMMAND" =~ _pyenv_virtualenv_hook ]]; then
PROMPT_COMMAND="_pyenv_virtualenv_hook;$PROMPT_COMMAND";
fi

$ pipenv --completion
_pipenv_completion() {
local IFS=$'\t'
COMPREPLY=( $( env COMP_WORDS="${COMP_WORDS[*]}" \
COMP_CWORD=$COMP_CWORD \
_PIPENV_COMPLETE=complete-bash $1 ) )
return 0
}

complete -F _pipenv_completion -o default pipenv

-------------------- end at here ----------------

I just want to know whether in this case, it's not feasible for running
them in .profile with eval.

Regards



David W. Hodgins

unread,
Dec 16, 2019, 4:19:49 PM12/16/19
to
On Mon, 16 Dec 2019 08:01:22 -0500, Hongyi Zhao <hongy...@gmail.com> wrote:

> OTOH, if I put the above code into ~/.bashrc,
> it will do the trick.
> So, I want to know whether should I put the eval command into .profile
> for this purpose?

Commands in .bashrc are executed at the start of each bash shell, so everytime
konsole or gnome-terminal is started those commands are run.

The .profile file is only sourced at login, so for changes there to take effect
the user must logout/in.

Regards, Dave Hodgins

--
Change dwho...@nomail.afraid.org to davidw...@teksavvy.com for
email replies.

Barry Margolin

unread,
Dec 17, 2019, 12:01:39 PM12/17/19
to
In article <op.0cwtiym...@hodgins.homeip.net>,
"David W. Hodgins" <dwho...@nomail.afraid.org> wrote:

> On Mon, 16 Dec 2019 08:01:22 -0500, Hongyi Zhao <hongy...@gmail.com>
> wrote:
>
> > OTOH, if I put the above code into ~/.bashrc,
> > it will do the trick.
> > So, I want to know whether should I put the eval command into .profile
> > for this purpose?
>
> Commands in .bashrc are executed at the start of each bash shell, so
> everytime
> konsole or gnome-terminal is started those commands are run.
>
> The .profile file is only sourced at login, so for changes there to take
> effect
> the user must logout/in.

In the old days of ordinary terminal logins, you would put terminal
settings and environment variables in .profile. Environment variables
would then be inherited by all the child processes in the session. And
there's just a single terminal session, so that only needs to be done
once.

Window systems changed that. Now you need terminal settings for each
terminal window that's opened. And the shells are not descendants of
each other, so they don't inherit the environment from each other.

I think you can use .xinitrc to set environment variables that will be
inherited by all processes started from the window manager.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

Helmut Waitzmann

unread,
Dec 17, 2019, 5:42:53 PM12/17/19
to
Hongyi Zhao <hongy...@gmail.com>:
>On Mon, 16 Dec 2019 13:01:22 +0000, Hongyi Zhao wrote:
>
>> eval "$(pyenv init -)"
>> eval "$(pyenv virtualenv-init -)"
>> eval "$(pipenv --completion)"
>
>I've done the test on the above commands in the (), and found the
>following results:
>
>Each of them will expanded into some very complex compound commands/
>functions as follows:
>
>
>-------------- begin from here --------------
>$ pyenv init -
>export PATH="/home/werner/.pyenv/shims:${PATH}"
>export PYENV_SHELL=bash
>source '/home/werner/.pyenv/libexec/../completions/pyenv.bash'
>command pyenv rehash 2>/dev/null
>pyenv() {

[…]

>}
>
>
>$ pyenv virtualenv-init -
>export PATH="/home/werner/.pyenv/plugins/pyenv-virtualenv/shims:${PATH}";
>export PYENV_VIRTUALENV_INIT=1;
>_pyenv_virtualenv_hook() {

[…]

>};

[…]

>
>-------------------- end at here ----------------
>

Thank you for showing the output.


>I just want to know whether in this case, it's not feasible for running
>them in .profile with eval.
>

It does make sense and it doesn't make sense to run those "eval"
commands from the file “~/.profile”:

The commands


“eval "$(pyenv init -)"”

and

“eval "$(pyenv virtualenv-init -)"”

both prepend something to the PATH environment variable. 
Therefore it's not very useful to let them run from “~/.bashrc”,
because then they will be run from each interactive (subshell)
bash.  For example, imagine, you have an interactive shell running
in a terminal or terminal emulator, then start an editor out of
it.  In the editor, you start an interactive shell.  Then this
shell will have the PATH environment variable extended twice by
the pyenv‐initializing commands.

=> The “eval "$(pyenv …)"” commands should better be run from
“~/.profile” rather than from „~/.bashrc“.

But now you'll get into another problem:


These initializing commands not only prepend something to the PATH
environment variable, but also define some functions.  But
functions (by default) are not exported to the environment.

=> If you put these commands for example into “~/.xinitrc”, the
environment will be initialized and then be inherited by any shell
running in a terminal window.  That's fine.  But the defined
functions will not be inherited by any shell running in a terminal
window.

=> The problem is, that the task done by the “eval "$(pyenv …)"”
commands should be split: into the environmental part, which
should be run from “~/.profile” or “~/.xinitrc”, and the function
definition part, which should be run from “~/.bashrc”.

As far as I can see you cannot do much to solve that problem
(apart from filing a bug report to the developers of the python
environment).

Hongyi Zhao

unread,
Dec 17, 2019, 10:52:32 PM12/17/19
to
On Tue, 17 Dec 2019 23:02:36 +0100, Helmut Waitzmann wrote:

> As far as I can see you cannot do much to solve that problem (apart from
> filing a bug report to the developers of the python environment).

Another issue, why you don't said the issue on:

eval "$(pipenv --completion)"

Which expanding into the following:

$ pipenv --completion
pyenv-implicit: found multiple pipenv in pyenv. Use version 3.5.9.

Helmut Waitzmann

unread,
Dec 18, 2019, 5:31:04 AM12/18/19
to
Hongyi Zhao <hongy...@gmail.com>:
>On Tue, 17 Dec 2019 23:02:36 +0100, Helmut Waitzmann wrote:
>
>> As far as I can see you cannot do much to solve that problem (apart from
>> filing a bug report to the developers of the python environment).
>
>Another issue, why you don't said the issue on:
>
> eval "$(pipenv --completion)"
>

Because I don't know anything about completion in the shell.

Jerry Peters

unread,
Dec 18, 2019, 4:29:13 PM12/18/19
to
xdm sources /etc/profile & ~/.profile in /etc/X11/Xsession.
Have no idea what other dm's do.
0 new messages