0

I have already referred to this post and post.

I would like to set few variables as global/system level environment variables which I would like to initialize once and be available anytime I login. I don't wish to reassign them again and again.

Below are what I did.

Step - 1 (in command line terminal)

export SPARK_HOME="/usr/spark"
export PATH="$PATH:/usr/spark/bin"
export PYTHONPATH="$SPARK_HOME/python:$SPARK_HOME/python/lib/py4j-0.10.7-src.zip:$PYTHONPATH"
export PATH="$SPARK_HOME/python:$PATH"

Step - 2 (in .bashrc file)

export SPARK_HOME="/usr/spark"
export PATH="$PATH:/usr/spark/bin"
export PYTHONPATH="$SPARK_HOME/python:$SPARK_HOME/python/lib/py4j-0.10.7-src.zip:$PYTHONPATH"
export PATH="$SPARK_HOME/python:$PATH"

step -2a refresh/reload bashrc file

source .bashrc

The above two steps didn't work. Meaning once I logout and login, when I issue printenv command, I don't see them

So, I thought of navigating to the below folder based on SO posts

cd /etc/environment -what should I do in this file?

cd /etc/profile - what should I do in this file?

cd /etc/profile.d/ - I see 4 files here as shown below (don't know what to do from here)

apps-bin-path.sh  bash_completion.sh  cedilla-portuguese.sh  gtk-accessibility.sh  vte-2.91.sh

My profile file looks like as below

if [ "$PS1" ]; then
  if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then
    # The file bash.bashrc already sets the default PS1.
    # PS1='\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
    fi
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
  fi
fi

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi

How can I set these environment variables for eternity? However many terminals I open, I would like to have them and use them.

The Great
  • 7,215
  • 7
  • 40
  • 128
  • Of course they are lost, when you log out. But when the shell is interactive, ~/.bashrc should be source. I suggest that you put a `set -x` temporarily as the first statement in your .bashrc, and open a new shell. You will get plenty of output, but you will see, what statements are executed. Please also post your bash version (`echo $BASH_VERSION`). – user1934428 Sep 15 '20 at 05:59
  • hi @user1934428 - Can't I set this variables forever? Meaning I will be using them everyday. How can I set this up in my profile? So, everytime I login, it is available irrespective of how many terminals I open? – The Great Sep 15 '20 at 06:01
  • @user1934428 - updated the post with `bash` and `profile` file details. One thing is, I am able to see `PYENV_ROOT` and `PATH` defined before the `If` clause – The Great Sep 15 '20 at 06:04
  • An environment variable, by definition, belongs to a **process**. When the process is gone, the environment dies with it. It is just inherited by the child processes. But you can of course provice a new, suitable environment for a new process. One purpose, why bash automatically processes certain files on startup, is to allow you to set up your environment. – user1934428 Sep 15 '20 at 06:07
  • Yes, thanks I understand now. so I would like to set up my environment for SPARK. Hence I require those variables listed to be available upon startup. I use the server only for SPARK. thats the purpose of our server.. – The Great Sep 15 '20 at 06:09
  • I have never worked with `pyenv`, but the way you use it, looks odd to me: AFIK, one thing `pyenv` is doing, is to modify your PATH. But you are running `pyenv` in a child process, which means that you don't see these changes. Well, if `pyenv` just outputs the commands for environment manipulation to stdout, this would work. In case you don't see the expected effect from `pyenv`, I suggest you post a new question for this topic, and appropriate tags. – user1934428 Sep 15 '20 at 06:13
  • Actually my question is not about `pyenv`. Its only about `setting environment variables` which is available everytime I login (in any number of terminals) – The Great Sep 15 '20 at 06:14
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/221490/discussion-between-user1934428-and-the-great). – user1934428 Sep 15 '20 at 06:14

1 Answers1

0

Read this post.

/etc/environment - what should I do in this file?

This is a system-wide configuration file, so it's used by all users on the system. You need 'root' permissions to edit it.

/etc/profile - what should I do in this file?

This is your personal shell initialization script.

Also, you're not the only one who asked this question before in regards to Sparky. See this post.

Kevin C
  • 4,851
  • 8
  • 30
  • 64