Results 1 to 4 of 4

Thread: Where to set environment variables for regular user?

  1. #1
    Join Date
    Jun 2007
    Beans
    129

    Where to set environment variables for regular user?

    Hi:

    I've been setting environment variables in the file /etc/environment, which has worked fine. However, I'd like to stop doing this because I'd like to have all of my settings in my home directory.

    1. Which file (in my home directory) should I set my environment variables in so that they get set whenever I log in? I presume that the value of a particular environment variable set in my home directory overrides the value set in /etc/environment. For example, if I set A="foo", would this override A="bar" in /etc/environment?
    2. In the file for the above question, how would I modify an existing environment variable? For example, how would I append paths to the $PATH variable?


    Thanks,
    LK

  2. #2
    Join Date
    Nov 2005
    Location
    Uppsala, Sweden
    Beans
    2,180
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: Where to set environment variables for regular user?

    Quote Originally Posted by LordKelvan View Post
    Hi:

    I've been setting environment variables in the file /etc/environment, which has worked fine. However, I'd like to stop doing this because I'd like to have all of my settings in my home directory.

    1. Which file (in my home directory) should I set my environment variables in so that they get set whenever I log in? I presume that the value of a particular environment variable set in my home directory overrides the value set in /etc/environment. For example, if I set A="foo", would this override A="bar" in /etc/environment?
    2. In the file for the above question, how would I modify an existing environment variable? For example, how would I append paths to the $PATH variable?


    Thanks,
    LK
    You can add paths in your ~/.bashrc
    Code:
    gedit ~/.bashrc
    If you want to include ~/.bin in your path, add this to the end:
    Code:
    if [ -d ~/.bin ]; then
        PATH=~/.bin:"${PATH}"
    fi
    You can also add aliases, either directly in ~/.bashrc or in ~/.bash_aliases:
    Code:
    alias A='foo'
    I'm pretty sure those aliases set in your ~/.bashrc or ./bash_aliases will override any aliases set anywhere else.

    To apply the changes without logging out:
    Code:
    source ~.bashrc
    Last edited by jocko; November 10th, 2009 at 08:20 AM.

  3. #3
    Join Date
    Jun 2007
    Beans
    129

    Re: Where to set environment variables for regular user?

    Sorry, I should have been more clear that
    Code:
    A="foo"
    was setting the environment variable A to "foo", and not setting up an alias.

    To confirm, I only need to edit ~/.bashrc. If I wanted to set an environment variable, I would add:
    Code:
    A="foo"
    If I want to append something to an existing environment variable, I would add (omitting the checking code):
    Code:
    A="${foo}":/path/to/somewhere
    Can I set it anywhere inside ~/.bashrc, or is there a specific place?

    One final thing: does Ubuntu first read /etc/environment for system-wide environment variables, and then read ~/.bashrc for user-specific environment variables (i.e., my changes will not be clobbered)?

  4. #4
    Join Date
    Nov 2005
    Location
    Uppsala, Sweden
    Beans
    2,180
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: Where to set environment variables for regular user?

    Quote Originally Posted by LordKelvan View Post
    Sorry, I should have been more clear that
    Code:
    A="foo"
    was setting the environment variable A to "foo", and not setting up an alias.

    To confirm, I only need to edit ~/.bashrc. If I wanted to set an environment variable, I would add:
    Code:
    A="foo"
    If I want to append something to an existing environment variable, I would add (omitting the checking code):
    Code:
    A="${foo}":/path/to/somewhere
    Can I set it anywhere inside ~/.bashrc, or is there a specific place?

    One final thing: does Ubuntu first read /etc/environment for system-wide environment variables, and then read ~/.bashrc for user-specific environment variables (i.e., my changes will not be clobbered)?
    Well... if I make the line:
    Code:
    A="foo"
    at the end of my ~/.bashrc, typing $A in the terminal gives me
    Code:
    No command 'foo' found, did you mean:
     Command 'xoo' from package 'xoo' (universe)
     Command 'fop' from package 'fop' (universe)
     Command 'fox' from package 'objcryst-fox' (universe)
     Command 'zoo' from package 'zoo' (universe)
     Command 'goo' from package 'goo' (universe)
    foo: command not found
    so it seems it works as you want it to.
    I don't think it matters where you place it.
    bashrc is read when you log in, so any variables in there should replace any variables that were set during boot.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •