Link to home
Start Free TrialLog in
Avatar of yongsing
yongsing

asked on

How to set environment variables

How do you set the PATH, JAVA_HOME or CLASSPATH in Redhat Linux?

I want to know how to set it such that when the OS is booted up, these variables are set. In Windows, this is done in the Control Panel -> System -> Advance -> Environment Variables.

I also want to know how you override the default variables in a particular shell. In Windows, we can do SET PATH=%PATH%;c:\newpath to override the default PATH.
SOLUTION
Avatar of da99rmd
da99rmd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of da99rmd
da99rmd

sry didnt read the the middle questione

put that in /etc/bashrc if bash

and /etc/csh.login for tcsh

this applays to all users if you just want i to your self put only in

%HOME%/.bashrc or
%HOME%/.tcshrc
type
env
to see what your env settings are and what shell you are using under SHELL

/Rob
da99rmd is right, another way is if you are setting this up for multiple users it can set in /etc/profile and it will be set for all system users at bootup
Avatar of yongsing

ASKER

I am using bash. Should I edit /etc/bashrc or etc/profile?

How do I add the settings in the file? This is how /etc/bashrc looks like:


# /etc/bashrc

# System wide functions and aliases
# Environment stuff goes in /etc/profile

# by default, we want this to get set.
# Even for non-interactive, non-login shells.
if [ "`id -gn`" = "`id -un`" -a `id -u` -gt 99 ]; then
      umask 002
else
      umask 022
fi

# are we an interactive shell?
if [ "$PS1" ]; then
    if [ -x /usr/bin/tput ]; then
      if [ "x`tput kbs`" != "x" ]; then # We can't do this with "dumb" terminal
        stty erase `tput kbs`
      elif [ -x /usr/bin/wc ]; then
        if [ "`tput kbs|wc -c `" -gt 0 ]; then # We can't do this with "dumb" terminal
          stty erase `tput kbs`
        fi
      fi
    fi
    case $TERM in
      xterm*)
            if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
                  PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
            else
                PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}\007"'
            fi
            ;;
      screen)
            if [ -e /etc/sysconfig/bash-prompt-screen ]; then
                  PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
            else
            PROMPT_COMMAND='echo -ne "\033_${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}\033\\"'
            fi
            ;;
      *)
            [ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default
          ;;
    esac
    # Turn on checkwinsize
    shopt -s checkwinsize
    [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
   
    if [ "x$SHLVL" != "x1" ]; then # We're not a login shell
        for i in /etc/profile.d/*.sh; do
          if [ -r "$i" ]; then
              . $i
          fi
      done
    fi
fi
# vim:ts=4:sw=4
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This sounds really confusing. I just need to know the Linux's equivalent to Windows setting via Control Panel -> System -> Advance -> Environment Variables.

It would be good if you can show me what to add to the relevant file.
edit your ~/.bashrc with your favourite editor
add those export commands to the end of the file, that da99rmd told you about up in his first post.  
save, exit the editor.  
log out, log back in.

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
majorwoo all show you the way, only that you have to login/get into a console mode (dos mode like) and use your editor to edit the file /etc/profile as majorwoo suggested.

and you have to login as root (the super user of linux/unix)

Im not realy in to windows but if the settings you are talking about in control pannel apply to all users or you want it to work that way put it in

/etc/bashrc

And if you just want the settings to the user you are using put it in your home directory in the file

.bashrc

/Rob
In Windows you open up a graphical dialogue, & click the "environment variables" button.  In there you can set variables for all users (known in MS Windows as "system variables") or just for the currently logged-in user ("user variables" in Windows)

There's only one right way to do it in MS Windows. It doesn't always work, but at least you know you tried the right thing. :-)

With Linux you gain the freedom to do it different ways, but at the price of reading lots of turgid manuals, and learning the several ways to configure things.  It might seem like a pain at first to learn this stuff, yongsing, but it's definitely worth it in the end.

da99rmd,

/etc/bashrc woud apply to all users using the bash shell (which is indeed the default for many linux systems) however if a user is using a different shell /etc/bashrc would be ignored.  Take a look at your /etc/passwd file, you will see  something like:
guest:x:509:500::/home/guest:/bin/bash
indicating the user guest, uses the bash shell.  If this here changed (if the user used a different shell such as /bin/csh, or alternatively if they launched their own new shell once logging in anythign entered in /etc/bashrc would not be read by that shell)

/etc/profile on the other hand is read by all user shells regardless of what the user has set
jdfox, but then you end up lerning somthing on the way when you are using linux :)

majorwoo, thats correct but most users dont chsh :/
for sure, I'm just arguing principle now.  It "is" closer to the windows way ;-)
Thanks guys!
Modify the file /etc/csh.login

something like this:

setenv PATH ".:/bin:/usr/bin:/usr/local/bin"   <--note the . which sets up local dir in path

setenv JAVA_HOME "/opt/myjava"


to edit that path later use the following:

setenv PATH ".:${PATH}:/somewhere/new"