How do I set a user environment variable? (permanently, not session)

  • This is irritating me. I seen several suggestions (all using different files and syntax) and none of them worked.

    How do I set an environment variable for a specific user? I am on debian squeeze. What is the exact syntax I should put in the file to make ABC = "123"?

    What did you try already?

    editing .bashrc, .bash_profile, .profile and .ssh/eviroment. Although i dont know if i wrote it in the right location or had the right syntax each time

    `.bashrc` is the main place for that.

    @rozcietrzewiacz: ok but... even after having the solution i tried setting ABC1 and ABC2 at the start and end of that file and it isnt set in my environment.

    Check that `.bash_profile` contains something like `[[ -f ~/.bashrc ]] && . ~/.bashrc`.

    what the heck!? i tried ssh-ing in and the variables i set in `.bashrc` show but not the ones in `.bash_profile`! ok, easy fix but... i dont understand why this is happening.

    @rozcietrzewiacz: thanks. Somehow i notice that line adds in bashrc UNLESS i stick a new line (making it 2lines with the second one empty). I'm clueless but it works with one line so i am happy

    This is not a full answer, but if it's shell-specific, I recommend putting it in `.bashrc`; if it's not, put it in `.profile`. I source `.profile` and `.bashrc` (in that order) from `.bash_profile`, and make sure to put `[[ $- != *i* ]] && return` on the top of `.bashrc`.

    // , Any way to do this without putting the values in a disk file?

  • Matteo

    Matteo Correct answer

    9 years ago

    You have to put the declaration in the initialization files of your shell:

    • If you are using bash, ash, ksh or some other Bourne-style shell, you can add

      ABC="123"; export ABC
      

      in your .profile file (${HOME}/.profile). This is the default situation on most Unix installations, and in particular on Debian.

      If your login shell is bash, you can use .bash_profile (${HOME}/.bash_profile) or .bash_login instead.

      Note: If either of these files exists and your login shell is bash, .profile is not read when you log in over ssh or on a text console, but it might still be read instead of .bash_profile if you log in from the GUI. Also, if there is no .bash_profile, then use .bashrc.

    • If you've set zsh as your login shell, use ~/.zprofile instead of ~/.profile.

    • If you are using tcsh, add

      setenv ABC "123"
      

      in .login file (${HOME}/.login)

    • if you are using another shell look at the shell manual how to define environment variables and which files are executed at the shell startup.

    that was one of the things i tried. i stuck `export ABC="123"` at the end of ~/.profile right now, exit (to root) then su back into that user and it didnt work. I checked by writing env and echo $ABC -edit- i am using bash. But i think the shell is a softlink located at `/bin/sh`

    The default behavior of `su user` is to change ownership of the current shell to the username arg, while keeping the home directory and environment of the calling user. To enter a shell in such a way as to become the new user, and gain all of their environmental settings ( $PATH, $HOME, $ABC, etc) you need to pass a - as the first argument to su. `su - username` will accomplish what you are asking for.

    Wait: what do you mean by "exited to root"? In any case making an 'su' does **not** initialize the shell. You need 'su -' (see `man su`)

    oh wow i didnt know that. Thanks @TimKennedy. However i still dont see the variable i set when using `su - theuser` :(

    To check if your .bashrc (or .profile) is correct: `source .bashrc`. Is then the variable set?

    Why did you `su` in the first place? Didn't you just create a root-owned `.profile` that cannot be used by the user?

    @Matteo ?. It returns me nothing. No error, no msg, nothing. Although if i write `export VAR="val"` at the end of `.bash_profile` which i had to create and use su - user i do get results :D

    @acid this is what is *should* have done. The question is, what is sourced when you log in. To check that, log *out* first.

    Because `.profile` is read by all Bourne-compatible shells, you should not write Bash syntax in that file. In particular, `export VARIABLE=value` needs to be refactored into `VARIABLE=value; export VARIABLE`.

    In Fedora: "/home/joshnoe/.profile: No such file or directory"

    @JoshNoe if it does not exist create it

    I use `zsh`. Will the `.zprofile` as describe above be read before `.zshrc` when `login` or when doing `exec zsh`?

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM