Reputation: 2502
I have a bash script that installs rbenv
to /usr/local
and adds the required environment variables to /etc/profile
. My user is root
:
#!/bin/bash
exec {log_fd}>log.txt
git clone https://github.com/rbenv/rbenv.git /usr/local/rbenv >&$log_fd 2>&1
git clone https://github.com/rbenv/ruby-build.git /usr/local/rbenv/plugins/ruby-build >&$log_fd 2>&1
echo 'export RBENV_ROOT=/usr/local/rbenv' >> /etc/profile >&$log_fd 2>&1
echo 'export PATH="/usr/local/rbenv/bin:$PATH"' >> /etc/profile >&$log_fd 2>&1
echo 'if which rbenv > /dev/null; then eval "$(rbenv init - --no-rehash)"; fi' >> /etc/profile >&$log_fd 2>&1
. /etc/profile
However, the PATH variable isn't being exported as it should, but RBENV_ROOT is!
~#: printenv
RBENV_ROOT=/usr/local/rbenv
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
MAIL=/var/mail/root
LANG=en_US.UTF-8
RBENV_SHELL=bash
I would expect to see /usr/local/rbenv/
as part of the PATH, right?
I've also tried switching up the syntax for adding the path but that didn't help either:
echo 'export PATH=$PATH:/usr/local/rbenv/bin' >> /etc/profile 2>&1
In case you're wondering, I'm redirecting the output because this is all within a whiptail menu.
So, why would one variable be shown RBENV_ROOT
but not my supposedly updated PATH
?
Upvotes: 1
Views: 1607
Reputation: 2362
The sequence >>/etc/profile >&$log_fd 2>&1
causes the echo
to run with stdout and stderr redirected to file descriptor $log_fd
, not to /etc/profile
. That's because the redirections are processed left-to-right in this case, and the second overwrites the first when it comes to stdout. To see this:
#!/bin/bash
exec {fd}>/tmp/file1
echo 'hello' >>/tmp/file2 >&$fd 2>&1
Make this executable, run it, then check that hello
appears only in /tmp/file1
.
Of course, this doesn't explain why RBENV_ROOT
is set. Perhaps you set it in some other startup file?
As for using echo 'export PATH=$PATH:/usr/local/rbenv/bin' >>/etc/profile'
, I can confirm that if I do that on Ubuntu 14.04, I see the new dir as part of PATH
, as expected. However, to see the change, you must make sure /etc/profile
gets sourced, which only happens if you start a login shell. E.g., env -i bash -lc 'echo $PATH'
.
Upvotes: 2